journald-server.c revision fe1abefcd3bf1718dde3b5b835db56142c9f7082
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/***
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2011 Lennart Poettering
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer under the terms of the GNU Lesser General Public License as published by
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt (at your option) any later version.
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt systemd is distributed in the hope that it will be useful, but
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt WITHOUT ANY WARRANTY; without even the implied warranty of
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1c36b4a73b876258fbe01fbe9bc9b750b7dcc9ceEvgeny Vereshchagin Lesser General Public License for more details.
c50a4525aa8151b180d5a325e88c5f3812e66c36Martin Pitt
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier You should have received a copy of the GNU Lesser General Public License
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier***/
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <sys/signalfd.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <sys/ioctl.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <linux/sockios.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <sys/statvfs.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <sys/mman.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <sys/timerfd.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <libudev.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <systemd/sd-journal.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <systemd/sd-messages.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <systemd/sd-daemon.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "fileio.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "mkdir.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "hashmap.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "journal-file.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "socket-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "cgroup-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "list.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "virt.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "missing.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "conf-parser.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "journal-internal.h"
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier#include "journal-vacuum.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journal-authenticate.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journald-server.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journald-rate-limit.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journald-kmsg.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journald-syslog.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journald-stream.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journald-console.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer#include "journald-native.h"
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin#ifdef HAVE_ACL
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <sys/acl.h>
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin#include <acl/libacl.h>
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin#include "acl-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#endif
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#ifdef HAVE_SELINUX
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <selinux/selinux.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#endif
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#define USER_JOURNALS_MAX 1024
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt#define DEFAULT_SYNC_INTERVAL_USEC (5*USEC_PER_MINUTE)
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#define DEFAULT_RATE_LIMIT_BURST 200
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic const char* const storage_table[] = {
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack [STORAGE_AUTO] = "auto",
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [STORAGE_VOLATILE] = "volatile",
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [STORAGE_PERSISTENT] = "persistent",
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [STORAGE_NONE] = "none"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier};
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny ChevalierDEFINE_STRING_TABLE_LOOKUP(storage, Storage);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny ChevalierDEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic const char* const split_mode_table[] = {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [SPLIT_NONE] = "none",
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack [SPLIT_UID] = "uid",
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack [SPLIT_LOGIN] = "login"
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack};
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-SzmekDEFINE_STRING_TABLE_LOOKUP(split_mode, SplitMode);
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-SzmekDEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode, split_mode, SplitMode, "Failed to parse split mode setting");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic uint64_t available_space(Server *s) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier char ids[33];
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *p = NULL;
24be78d72b931b0175f08cee12fd23d631c024bfEvgeny Vereshchagin const char *f;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_id128_t machine;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier struct statvfs ss;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier uint64_t sum = 0, avail = 0, ss_avail = 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_closedir_ DIR *d = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier usec_t ts;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier JournalMetrics *m;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier ts = now(CLOCK_MONOTONIC);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return s->cached_available_space;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_id128_get_machine(&machine);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->system_journal) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier f = "/var/log/journal/";
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier m = &s->system_metrics;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier f = "/run/log/journal/";
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier m = &s->runtime_metrics;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(m);
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin p = strappend(f, sd_id128_to_string(machine, ids));
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin if (!p)
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin return 0;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin d = opendir(p);
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin if (!d)
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin return 0;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin if (fstatvfs(dirfd(d), &ss) < 0)
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin return 0;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin for (;;) {
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin struct stat st;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin struct dirent *de;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin union dirent_storage buf;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin r = readdir_r(d, &buf.de, &de);
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin if (r != 0)
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin break;
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin if (!de)
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin break;
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin if (!endswith(de->d_name, ".journal") &&
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin !endswith(de->d_name, ".journal~"))
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin continue;
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin continue;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!S_ISREG(st.st_mode))
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier continue;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sum += (uint64_t) st.st_blocks * 512UL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier avail = sum >= m->max_use ? 0 : m->max_use - sum;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek ss_avail = ss.f_bsize * ss.f_bavail;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
2375607039517c88df51ef16ddbb624ec1c10654Kay Sievers ss_avail = ss_avail < m->keep_free ? 0 : ss_avail - m->keep_free;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (ss_avail < avail)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier avail = ss_avail;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier s->cached_available_space = avail;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier s->cached_available_space_timestamp = ts;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return avail;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void server_read_file_gid(Server *s) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *g = "systemd-journal";
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(s);
1b1eae69ce52ef6c89a1200e8d3758549b291991Daniel Mack
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->file_gid_valid)
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek return;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
edbced8a151c1b7ded685e2ec644950d2adec5f5Harald Hoyer r = get_group_creds(&g, &s->file_gid);
1b1eae69ce52ef6c89a1200e8d3758549b291991Daniel Mack if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to resolve '%s' group: %s", g, strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* if we couldn't read the gid, then it will be 0, but that's
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek * fine and we shouldn't try to resolve the group again, so
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * let's just pretend it worked right-away. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier s->file_gid_valid = true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevaliervoid server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier#ifdef HAVE_ACL
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier acl_t acl;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier acl_entry_t entry;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier acl_permset_t permset;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier#endif
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier server_read_file_gid(s);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = fchmod_and_fchown(f->fd, 0640, 0, s->file_gid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier#ifdef HAVE_ACL
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (uid <= 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier acl = acl_get_fd(f->fd);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!acl) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin }
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin r = acl_find_uid(acl, uid, &entry);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin if (r <= 0) {
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin if (acl_create_entry(&acl, &entry) < 0 ||
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin acl_set_tag_type(entry, ACL_USER) < 0 ||
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin acl_set_qualifier(entry, &uid) < 0) {
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin goto finish;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* We do not recalculate the mask unconditionally here,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * so that the fchmod() mask above stays intact. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (acl_get_permset(entry, &permset) < 0 ||
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier acl_add_perm(permset, ACL_READ) < 0 ||
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier calc_acl_mask_if_needed(&acl) < 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier goto finish;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (acl_set_fd(f->fd, acl) < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierfinish:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier acl_free(acl);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier#endif
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic JournalFile* find_journal(Server *s, uid_t uid) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier char *p;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier JournalFile *f;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_id128_t machine;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(s);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* We split up user logs only on /var, not on /run. If the
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * runtime file is open, we write to it exclusively, in order
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * to guarantee proper order as soon as we flush /run to
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * /var and close the runtime file. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->runtime_journal)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return s->runtime_journal;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
bf3a947cb44f31359bba313e0252cbcc0dc95b03Evgeny Vereshchagin if (uid <= 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return s->system_journal;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_id128_get_machine(&machine);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return s->system_journal;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (f)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return f;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/user-%lu.journal",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier SD_ID128_FORMAT_VAL(machine), (unsigned long) uid) < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return s->system_journal;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier while (hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Too many open? Then let's close one */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier f = hashmap_steal_first(s->user_journals);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier journal_file_close(f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, s->system_journal, &f);
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt free(p);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return s->system_journal;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier server_fix_perms(s, f, uid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0) {
cffae62bcb6912fbaf1b7b282d9d170c9d308897Martin Pitt journal_file_close(f);
99877b7e3782a51b31bf191825f0335500f52fe5Harald Hoyer return s->system_journal;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return f;
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt}
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevaliervoid server_rotate(Server *s) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier JournalFile *f;
e63b61be5350dbe92ea12e1eeb96dde251ed9292Evgeny Vereshchagin void *k;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Iterator i;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_debug("Rotating...");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->runtime_journal) {
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin r = journal_file_rotate(&s->runtime_journal, s->compress, false);
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->runtime_journal)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to rotate %s: %s", s->runtime_journal->path, strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to create new runtime journal: %s", strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin server_fix_perms(s, s->runtime_journal, 0);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin if (s->system_journal) {
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin r = journal_file_rotate(&s->system_journal, s->compress, s->seal);
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->system_journal)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to rotate %s: %s", s->system_journal->path, strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to create new system journal: %s", strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier server_fix_perms(s, s->system_journal, 0);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = journal_file_rotate(&f, s->compress, s->seal);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (f)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to rotate %s: %s", f->path, strerror(-r));
25b47f96d9601ff566257b2a31bfb5f4bd25d661Marko Myllynen else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to create user journal: %s", strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier hashmap_replace(s->user_journals, k, f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier server_fix_perms(s, f, PTR_TO_UINT32(k));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevaliervoid server_sync(Server *s) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier JournalFile *f;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier void *k;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Iterator i;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
53d90f9582f96208b3674da823ad1a3d2c3b1aa4Martin Pitt static const struct itimerspec sync_timer_disable = {};
5c404f1ab8e96efedb983806443ca982a1b2a372Evgeny Vereshchagin
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (s->system_journal) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = journal_file_set_offline(s->system_journal);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to sync system journal: %s", strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = journal_file_set_offline(f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to sync user journal: %s", strerror(-r));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = timerfd_settime(s->sync_timer_fd, 0, &sync_timer_disable, NULL);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error("Failed to disable max timer: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier s->sync_scheduled = false;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevaliervoid server_vacuum(Server *s) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier char *p;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier char ids[33];
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer sd_id128_t machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_debug("Vacuuming...");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer s->oldest_file_usec = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_id128_get_machine(&machine);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to get machine ID: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_id128_to_string(machine, ids);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->system_journal) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer p = strappend("/var/log/journal/", ids);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!p) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_oom();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free, s->max_retention_usec, &s->oldest_file_usec);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0 && r != -ENOENT)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to vacuum %s: %s", p, strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(p);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->runtime_journal) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer p = strappend("/run/log/journal/", ids);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!p) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_oom();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free, s->max_retention_usec, &s->oldest_file_usec);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0 && r != -ENOENT)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to vacuum %s: %s", p, strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(p);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer s->cached_available_space_timestamp = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerbool shall_try_append_again(JournalFile *f, int r) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* -E2BIG Hit configured limit
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier -EFBIG Hit fs limit
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier -EDQUOT Quota limit hit
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier -ENOSPC Disk full
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier -EHOSTDOWN Other machine
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier -EBUSY Unclean shutdown
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer -EPROTONOSUPPORT Unsupported feature
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer -EBADMSG Corrupted
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer -ENODATA Truncated
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer -ESHUTDOWN Already archived */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == -E2BIG || r == -EFBIG || r == -EDQUOT || r == -ENOSPC)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_debug("%s: Allocation limit reached, rotating.", f->path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (r == -EHOSTDOWN)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_info("%s: Journal file from other machine, rotating.", f->path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (r == -EBUSY)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_info("%s: Unclean shutdown, rotating.", f->path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (r == -EPROTONOSUPPORT)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_info("%s: Unsupported feature, rotating.", f->path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (r == -EBADMSG || r == -ENODATA || r == ESHUTDOWN)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("%s: Journal file corrupted, rotating.", f->path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return false;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return true;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer JournalFile *f;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer bool vacuumed = false;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(iovec);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n > 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = find_journal(s, uid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!f)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (journal_file_rotate_suggested(f, s->max_file_usec)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_debug("%s: Journal header limits reached or header out-of-date, rotating.", f->path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_rotate(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_vacuum(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer vacuumed = true;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = find_journal(s, uid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!f)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_schedule_sync(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (vacuumed || !shall_try_append_again(f, r)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to write entry, ignoring: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_rotate(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_vacuum(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = find_journal(s, uid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!f)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_debug("Retrying write.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to write entry, ignoring: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void dispatch_message_real(
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Server *s,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct iovec *iovec, unsigned n, unsigned m,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct ucred *ucred,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct timeval *tv,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *label, size_t label_len,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *unit_id) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char pid[sizeof("_PID=") + DECIMAL_STR_MAX(pid_t)],
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer uid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)],
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer gid[sizeof("_GID=") + DECIMAL_STR_MAX(gid_t)],
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer owner_uid[sizeof("_SYSTEMD_OWNER_UID=") + DECIMAL_STR_MAX(uid_t)],
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer source_time[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)],
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer boot_id[sizeof("_BOOT_ID=") + 32] = "_BOOT_ID=",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_id[sizeof("_MACHINE_ID=") + 32] = "_MACHINE_ID=";
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *comm, *exe, *cmdline, *cgroup, *session, *unit, *hostname;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_id128_t id;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *t, *c;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer uid_t realuid = 0, owner = 0, journal_uid;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer bool owner_valid = false;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#ifdef HAVE_AUDIT
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char audit_session[sizeof("_AUDIT_SESSION=") + DECIMAL_STR_MAX(uint32_t)],
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer audit_loginuid[sizeof("_AUDIT_LOGINUID=") + DECIMAL_STR_MAX(uid_t)];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer uint32_t audit;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer uid_t loginuid;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer#endif
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(iovec);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n > 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n + N_IOVEC_META_FIELDS <= m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ucred) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer realuid = ucred->uid;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sprintf(pid, "_PID=%lu", (unsigned long) ucred->pid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], pid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sprintf(uid, "_UID=%lu", (unsigned long) ucred->uid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], uid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sprintf(gid, "_GID=%lu", (unsigned long) ucred->gid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], gid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = get_process_comm(ucred->pid, &t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer comm = strappenda("_COMM=", t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], comm);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = get_process_exe(ucred->pid, &t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer exe = strappenda("_EXE=", t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], exe);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = get_process_cmdline(ucred->pid, 0, false, &t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer cmdline = strappenda("_CMDLINE=", t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], cmdline);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#ifdef HAVE_AUDIT
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = audit_session_from_pid(ucred->pid, &audit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sprintf(audit_session, "_AUDIT_SESSION=%lu", (unsigned long) audit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], audit_session);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = audit_loginuid_from_pid(ucred->pid, &loginuid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sprintf(audit_loginuid, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], audit_loginuid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#endif
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = cg_pid_get_path_shifted(ucred->pid, NULL, &c);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer cgroup = strappenda("_SYSTEMD_CGROUP=", c);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], cgroup);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = cg_path_get_session(c, &t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer session = strappenda("_SYSTEMD_SESSION=", t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], session);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (cg_path_get_owner_uid(c, &owner) >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer owner_valid = true;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sprintf(owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], owner_uid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (cg_path_get_unit(c, &t) >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit = strappenda("_SYSTEMD_UNIT=", t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (cg_path_get_user_unit(c, &t) >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit = strappenda("_SYSTEMD_USER_UNIT=", t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (unit_id) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (session)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit = strappenda("_SYSTEMD_USER_UNIT=", unit_id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit = strappenda("_SYSTEMD_UNIT=", unit_id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (unit)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], unit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(c);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#ifdef HAVE_SELINUX
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (label) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *selinux_context = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *((char*) mempcpy(stpcpy(selinux_context, "_SELINUX_CONTEXT="), label, label_len)) = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], selinux_context);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer security_context_t con;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (getpidcon(ucred->pid, &con) >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *selinux_context = strappenda("_SELINUX_CONTEXT=", con);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer freecon(con);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], selinux_context);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#endif
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (tv) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sprintf(source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu", (unsigned long long) timeval_load(tv));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], source_time);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Note that strictly speaking storing the boot id here is
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * redundant since the entry includes this in-line
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * anyway. However, we need this indexed, too. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_id128_get_boot(&id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_id128_to_string(id, boot_id + sizeof("_BOOT_ID=") - 1);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], boot_id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_id128_get_machine(&id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_id128_to_string(id, machine_id + sizeof("_MACHINE_ID=") - 1);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], machine_id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer t = gethostname_malloc();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (t) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer hostname = strappenda("_HOSTNAME=", t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(t);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], hostname);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n <= m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->split_mode == SPLIT_UID && realuid > 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Split up strictly by any UID */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer journal_uid = realuid;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (s->split_mode == SPLIT_LOGIN && realuid > 0 && owner_valid && owner > 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Split up by login UIDs, this avoids creation of
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * individual journals for system UIDs. We do this
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * only if the realuid is not root, in order not to
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * accidentally leak privileged information to the
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * user that is logged by a privileged process that is
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * part of an unprivileged session.*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer journal_uid = owner;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer journal_uid = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer write_to_journal(s, journal_uid, iovec, n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyervoid server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char mid[11 + 32 + 1];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char buffer[16 + LINE_MAX + 1];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct iovec iovec[N_IOVEC_META_FIELDS + 4];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int n = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer va_list ap;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct ucred ucred = {};
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(format);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], "PRIORITY=6");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer memcpy(buffer, "MESSAGE=", 8);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer va_start(ap, format);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer va_end(ap);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char_array_0(buffer);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], buffer);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer snprintf(mid, sizeof(mid), MESSAGE_ID(message_id));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char_array_0(mid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer IOVEC_SET_STRING(iovec[n++], mid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ucred.pid = getpid();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ucred.uid = getuid();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ucred.gid = getgid();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyervoid server_dispatch_message(
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Server *s,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct iovec *iovec, unsigned n, unsigned m,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct ucred *ucred,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct timeval *tv,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *label, size_t label_len,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *unit_id,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int priority) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int rl, r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ char *path = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *c;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(iovec || n == 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (LOG_PRI(priority) > s->max_level_store)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!ucred)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto finish;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = cg_pid_get_path_shifted(ucred->pid, NULL, &path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto finish;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* example: /user/lennart/3/foobar
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * /system/dbus.service/foobar
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * So let's cut of everything past the third /, since that is
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * where user directories start */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer c = strchr(path, '/');
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (c) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer c = strchr(c+1, '/');
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (c) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer c = strchr(c+1, '/');
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (c)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *c = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer rl = journal_rate_limit_test(s->rate_limit, path,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer priority & LOG_PRIMASK, available_space(s));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (rl == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Write a suppression message if we suppressed something */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (rl > 1)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_driver_message(s, SD_MESSAGE_JOURNAL_DROPPED,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Suppressed %u messages from %s", rl - 1, path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfinish:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int system_journal_open(Server *s) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *fn;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_id128_t machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char ids[33];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer uint64_t avail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer avail = available_space(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_id128_get_machine(&machine);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_id128_to_string(machine, ids);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!s->system_journal &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer access("/run/systemd/journal/flushed", F_OK) >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If in auto mode: first try to create the machine
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * path, but not the prefix.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * If in persistent mode: create /var/log/journal and
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * the machine path */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->storage == STORAGE_PERSISTENT)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (void) mkdir("/var/log/journal/", 0755);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fn = strappend("/var/log/journal/", ids);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!fn)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (void) mkdir(fn, 0755);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(fn);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fn = strjoin("/var/log/journal/", ids, "/system.journal", NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!fn)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(fn);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char fb[FORMAT_BYTES_MAX];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_fix_perms(s, s->system_journal, 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_driver_message(s, SD_ID128_NULL, "Allowing system journal files to grow to %s.",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer format_bytes(fb, sizeof(fb), s->system_metrics.max_use));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->system_metrics.max_use > avail)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_driver_message(s, SD_ID128_NULL, "Journal size currently limited to %s due to SystemKeepFree.",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer format_bytes(fb, sizeof(fb), avail));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r != -ENOENT && r != -EROFS)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to open system journal: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!s->runtime_journal &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (s->storage != STORAGE_NONE)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!fn)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->system_journal) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Try to open the runtime journal, but only
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * if it already exists, so that we can flush
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * it into the system journal */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(fn);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r != -ENOENT)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to open runtime journal: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* OK, we really need the runtime journal, so create
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * it if necessary. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (void) mkdir_parents(fn, 0755);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(fn);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to open runtime journal: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->runtime_journal) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char fb[FORMAT_BYTES_MAX];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_fix_perms(s, s->runtime_journal, 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_driver_message(s, SD_ID128_NULL, "Allowing runtime journal files to grow to %s.",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer format_bytes(fb, sizeof(fb), s->runtime_metrics.max_use));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->system_metrics.max_use > avail)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_driver_message(s, SD_ID128_NULL, "Journal size currently limited to %s due to RuntimeKeepFree.",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer format_bytes(fb, sizeof(fb), avail));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint server_flush_to_var(Server *s) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_id128_t machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_journal *j = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->storage != STORAGE_AUTO &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer s->storage != STORAGE_PERSISTENT)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!s->runtime_journal)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer system_journal_open(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!s->system_journal)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_debug("Flushing to /var...");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_id128_get_machine(&machine);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to get machine id: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Failed to read runtime journal: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer sd_journal_set_data_threshold(j, 0);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer SD_JOURNAL_FOREACH(j) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Object *o = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer JournalFile *f;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer f = j->current_file;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(f && f->current_offset > 0);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Can't read entry: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer goto finish;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r >= 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!shall_try_append_again(s->system_journal, r)) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Can't write entry: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer goto finish;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer server_rotate(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer server_vacuum(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!s->system_journal) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_notice("Didn't flush runtime journal since rotation of system journal wasn't successful.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer goto finish;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_debug("Retrying write.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Can't write entry: %s", strerror(-r));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer goto finish;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerfinish:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer journal_file_post_change(s->system_journal);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer journal_file_close(s->runtime_journal);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer s->runtime_journal = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r >= 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer rm_rf("/run/log/journal", false, true, false);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer sd_journal_close(j);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerint process_event(Server *s, struct epoll_event *ev) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(ev);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ev->data.fd == s->signal_fd) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct signalfd_siginfo sfsi;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer ssize_t n;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ev->events != EPOLLIN) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Got invalid event from epoll.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
c53158818d8cdaf46b3f1b5299b9bda118a1043fThomas Hindoe Paaboel Andersen
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer n = read(s->signal_fd, &sfsi, sizeof(sfsi));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (n != sizeof(sfsi)) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (n >= 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (errno == EINTR || errno == EAGAIN)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (sfsi.ssi_signo == SIGUSR1) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer touch("/run/systemd/journal/flushed");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer server_flush_to_var(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer server_sync(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (sfsi.ssi_signo == SIGUSR2) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer server_rotate(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer server_vacuum(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_info("Received SIG%s", signal_to_string(sfsi.ssi_signo));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer } else if (ev->data.fd == s->sync_timer_fd) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uint64_t t;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_debug("Got sync request from epoll.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = read(ev->data.fd, (void *)&t, sizeof(t));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer server_sync(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer } else if (ev->data.fd == s->dev_kmsg_fd) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ev->events != EPOLLIN) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Got invalid event from epoll.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = server_read_dev_kmsg(s);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer } else if (ev->data.fd == s->native_fd ||
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer ev->data.fd == s->syslog_fd) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ev->events != EPOLLIN) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Got invalid event from epoll.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer for (;;) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct msghdr msghdr;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct iovec iovec;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct ucred *ucred = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct timeval *tv = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct cmsghdr *cmsg;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer char *label = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer size_t label_len = 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer union {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct cmsghdr cmsghdr;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* We use NAME_MAX space for the
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * SELinux label here. The kernel
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * currently enforces no limit, but
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * according to suggestions from the
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * SELinux people this will change and
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * it will probably be identical to
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * NAME_MAX. For now we use that, but
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * this should be updated one day when
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * the final limit is known.*/
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer CMSG_SPACE(sizeof(struct timeval)) +
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer CMSG_SPACE(sizeof(int)) + /* fd */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer CMSG_SPACE(NAME_MAX)]; /* selinux label */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer } control;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer ssize_t n;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int v;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int *fds = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer unsigned n_fds = 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ioctl(ev->data.fd, SIOCINQ, &v) < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("SIOCINQ failed: %m");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (s->buffer_size < (size_t) v) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer void *b;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer size_t l;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer b = realloc(s->buffer, l+1);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!b) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("Couldn't increase buffer.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -ENOMEM;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer s->buffer_size = l;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer s->buffer = b;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer zero(iovec);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer iovec.iov_base = s->buffer;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer iovec.iov_len = s->buffer_size;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer zero(control);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer zero(msghdr);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer msghdr.msg_iov = &iovec;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer msghdr.msg_iovlen = 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer msghdr.msg_control = &control;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer msghdr.msg_controllen = sizeof(control);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (n < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (errno == EINTR || errno == EAGAIN)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error("recvmsg() failed: %m");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (cmsg->cmsg_level == SOL_SOCKET &&
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer cmsg->cmsg_type == SCM_CREDENTIALS &&
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer ucred = (struct ucred*) CMSG_DATA(cmsg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (cmsg->cmsg_level == SOL_SOCKET &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer cmsg->cmsg_type == SCM_SECURITY) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer label = (char*) CMSG_DATA(cmsg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer label_len = cmsg->cmsg_len - CMSG_LEN(0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (cmsg->cmsg_level == SOL_SOCKET &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer cmsg->cmsg_type == SO_TIMESTAMP &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer tv = (struct timeval*) CMSG_DATA(cmsg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (cmsg->cmsg_level == SOL_SOCKET &&
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer cmsg->cmsg_type == SCM_RIGHTS) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fds = (int*) CMSG_DATA(cmsg);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ev->data.fd == s->syslog_fd) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *e;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n > 0 && n_fds == 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer e = memchr(s->buffer, '\n', n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (e)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *e = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer s->buffer[n] = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (n_fds > 0)
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer log_warning("Got file descriptors via syslog socket. Ignoring.");
2c393ed7611a586ef5665aa62453ec57c0f5fef6Evgeny Vereshchagin
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer } else {
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer if (n > 0 && n_fds == 0)
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer server_process_native_message(s, s->buffer, n, ucred, tv, label, label_len);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (n == 0 && n_fds == 1)
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer server_process_native_file(s, fds[0], ucred, tv, label, label_len);
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer else if (n_fds > 0)
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer log_warning("Got too many file descriptors via native socket. Ignoring.");
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer }
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer close_many(fds, n_fds);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 1;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (ev->data.fd == s->stdout_fd) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ev->events != EPOLLIN) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Got invalid event from epoll.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -EIO;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer stdout_stream_new(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 1;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer StdoutStream *stream;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Got invalid event from epoll.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -EIO;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If it is none of the well-known fds, it must be an
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * stdout stream fd. Note that this is a bit ugly here
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * (since we rely that none of the well-known fds
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * could be interpreted as pointer), but nonetheless
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * safe, since the well-known fds would never get an
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * fd > 4096, i.e. beyond the first memory page */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer stream = ev->data.ptr;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (stdout_stream_process(stream) <= 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer stdout_stream_free(stream);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 1;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Unknown event.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int open_signalfd(Server *s) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sigset_t mask;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct epoll_event ev;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert_se(sigemptyset(&mask) == 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -1);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->signal_fd < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("signalfd(): %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -errno;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
zero(ev);
ev.events = EPOLLIN;
ev.data.fd = s->signal_fd;
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
log_error("epoll_ctl(): %m");
return -errno;
}
return 0;
}
static int server_parse_proc_cmdline(Server *s) {
_cleanup_free_ char *line = NULL;
char *w, *state;
int r;
size_t l;
if (detect_container(NULL) > 0)
return 0;
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) {
_cleanup_free_ char *word;
word = strndup(w, l);
if (!word)
return -ENOMEM;
if (startswith(word, "systemd.journald.forward_to_syslog=")) {
r = parse_boolean(word + 35);
if (r < 0)
log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
else
s->forward_to_syslog = r;
} else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
r = parse_boolean(word + 33);
if (r < 0)
log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
else
s->forward_to_kmsg = r;
} else if (startswith(word, "systemd.journald.forward_to_console=")) {
r = parse_boolean(word + 36);
if (r < 0)
log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
else
s->forward_to_console = r;
} else if (startswith(word, "systemd.journald"))
log_warning("Invalid systemd.journald parameter. Ignoring.");
}
return 0;
}
static int server_parse_config_file(Server *s) {
static const char fn[] = "/etc/systemd/journald.conf";
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(s);
f = fopen(fn, "re");
if (!f) {
if (errno == ENOENT)
return 0;
log_warning("Failed to open configuration file %s: %m", fn);
return -errno;
}
r = config_parse(NULL, fn, f, "Journal\0", config_item_perf_lookup,
(void*) journald_gperf_lookup, false, false, s);
if (r < 0)
log_warning("Failed to parse configuration file: %s", strerror(-r));
return r;
}
static int server_open_sync_timer(Server *s) {
int r;
struct epoll_event ev;
assert(s);
s->sync_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
if (s->sync_timer_fd < 0)
return -errno;
zero(ev);
ev.events = EPOLLIN;
ev.data.fd = s->sync_timer_fd;
r = epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->sync_timer_fd, &ev);
if (r < 0) {
log_error("Failed to add idle timer fd to epoll object: %m");
return -errno;
}
return 0;
}
int server_schedule_sync(Server *s) {
int r;
assert(s);
if (s->sync_scheduled)
return 0;
if (s->sync_interval_usec) {
struct itimerspec sync_timer_enable = {};
timespec_store(&sync_timer_enable.it_value, s->sync_interval_usec);
r = timerfd_settime(s->sync_timer_fd, 0, &sync_timer_enable, NULL);
if (r < 0)
return -errno;
}
s->sync_scheduled = true;
return 0;
}
int server_init(Server *s) {
int n, r, fd;
assert(s);
zero(*s);
s->sync_timer_fd = s->syslog_fd = s->native_fd = s->stdout_fd =
s->signal_fd = s->epoll_fd = s->dev_kmsg_fd = -1;
s->compress = true;
s->seal = true;
s->sync_interval_usec = DEFAULT_SYNC_INTERVAL_USEC;
s->sync_scheduled = false;
s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
s->forward_to_syslog = true;
s->max_level_store = LOG_DEBUG;
s->max_level_syslog = LOG_DEBUG;
s->max_level_kmsg = LOG_NOTICE;
s->max_level_console = LOG_INFO;
memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
server_parse_config_file(s);
server_parse_proc_cmdline(s);
if (!!s->rate_limit_interval ^ !!s->rate_limit_burst) {
log_debug("Setting both rate limit interval and burst from %llu,%u to 0,0",
(long long unsigned) s->rate_limit_interval,
s->rate_limit_burst);
s->rate_limit_interval = s->rate_limit_burst = 0;
}
mkdir_p("/run/systemd/journal", 0755);
s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
if (!s->user_journals)
return log_oom();
s->mmap = mmap_cache_new();
if (!s->mmap)
return log_oom();
s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (s->epoll_fd < 0) {
log_error("Failed to create epoll object: %m");
return -errno;
}
n = sd_listen_fds(true);
if (n < 0) {
log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
return n;
}
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
if (s->native_fd >= 0) {
log_error("Too many native sockets passed.");
return -EINVAL;
}
s->native_fd = fd;
} else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
if (s->stdout_fd >= 0) {
log_error("Too many stdout sockets passed.");
return -EINVAL;
}
s->stdout_fd = fd;
} else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0) {
if (s->syslog_fd >= 0) {
log_error("Too many /dev/log sockets passed.");
return -EINVAL;
}
s->syslog_fd = fd;
} else {
log_error("Unknown socket passed.");
return -EINVAL;
}
}
r = server_open_syslog_socket(s);
if (r < 0)
return r;
r = server_open_native_socket(s);
if (r < 0)
return r;
r = server_open_stdout_socket(s);
if (r < 0)
return r;
r = server_open_dev_kmsg(s);
if (r < 0)
return r;
r = server_open_kernel_seqnum(s);
if (r < 0)
return r;
r = server_open_sync_timer(s);
if (r < 0)
return r;
r = open_signalfd(s);
if (r < 0)
return r;
s->udev = udev_new();
if (!s->udev)
return -ENOMEM;
s->rate_limit = journal_rate_limit_new(s->rate_limit_interval,
s->rate_limit_burst);
if (!s->rate_limit)
return -ENOMEM;
r = system_journal_open(s);
if (r < 0)
return r;
return 0;
}
void server_maybe_append_tags(Server *s) {
#ifdef HAVE_GCRYPT
JournalFile *f;
Iterator i;
usec_t n;
n = now(CLOCK_REALTIME);
if (s->system_journal)
journal_file_maybe_append_tag(s->system_journal, n);
HASHMAP_FOREACH(f, s->user_journals, i)
journal_file_maybe_append_tag(f, n);
#endif
}
void server_done(Server *s) {
JournalFile *f;
assert(s);
while (s->stdout_streams)
stdout_stream_free(s->stdout_streams);
if (s->system_journal)
journal_file_close(s->system_journal);
if (s->runtime_journal)
journal_file_close(s->runtime_journal);
while ((f = hashmap_steal_first(s->user_journals)))
journal_file_close(f);
hashmap_free(s->user_journals);
if (s->epoll_fd >= 0)
close_nointr_nofail(s->epoll_fd);
if (s->signal_fd >= 0)
close_nointr_nofail(s->signal_fd);
if (s->syslog_fd >= 0)
close_nointr_nofail(s->syslog_fd);
if (s->native_fd >= 0)
close_nointr_nofail(s->native_fd);
if (s->stdout_fd >= 0)
close_nointr_nofail(s->stdout_fd);
if (s->dev_kmsg_fd >= 0)
close_nointr_nofail(s->dev_kmsg_fd);
if (s->sync_timer_fd >= 0)
close_nointr_nofail(s->sync_timer_fd);
if (s->rate_limit)
journal_rate_limit_free(s->rate_limit);
if (s->kernel_seqnum)
munmap(s->kernel_seqnum, sizeof(uint64_t));
free(s->buffer);
free(s->tty_path);
if (s->mmap)
mmap_cache_unref(s->mmap);
if (s->udev)
udev_unref(s->udev);
}