journald-server.c revision 23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2011 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
8bdbb8d9cbe1d35708385573d70984ab4533812dLennart Poettering#include <sys/signalfd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/ioctl.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <linux/sockios.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/statvfs.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/mman.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/timerfd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include <libudev.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include "sd-journal.h"
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#include "sd-messages.h"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#include "sd-daemon.h"
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering#include "fileio.h"
d538bfc71331d2e48363283decd77a61599021dbCristian Rodríguez#include "mkdir.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "hashmap.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "journal-file.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "socket-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "cgroup-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "list.h"
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#include "missing.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "conf-parser.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "selinux-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "journal-internal.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "journal-vacuum.h"
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering#include "journal-authenticate.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "journald-rate-limit.h"
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#include "journald-kmsg.h"
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering#include "journald-syslog.h"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include "journald-stream.h"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering#include "journald-console.h"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#include "journald-native.h"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#include "journald-audit.h"
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering#include "journald-server.h"
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering#ifdef HAVE_ACL
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/acl.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <acl/libacl.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "acl-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen#ifdef HAVE_SELINUX
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <selinux/selinux.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define USER_JOURNALS_MAX 1024
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#define DEFAULT_SYNC_INTERVAL_USEC (5*USEC_PER_MINUTE)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering#define DEFAULT_RATE_LIMIT_INTERVAL (30*USEC_PER_SEC)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#define DEFAULT_RATE_LIMIT_BURST 1000
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#define DEFAULT_MAX_FILE_USEC USEC_PER_MONTH
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering#define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic const char* const storage_table[_STORAGE_MAX] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [STORAGE_AUTO] = "auto",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [STORAGE_VOLATILE] = "volatile",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [STORAGE_PERSISTENT] = "persistent",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [STORAGE_NONE] = "none"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(storage, Storage);
1ee306e1248866617c96ed9f4263f375588ad838Lennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic const char* const split_mode_table[_SPLIT_MAX] = {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering [SPLIT_LOGIN] = "login",
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering [SPLIT_UID] = "uid",
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering [SPLIT_NONE] = "none",
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering};
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(split_mode, SplitMode);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode, split_mode, SplitMode, "Failed to parse split mode setting");
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic uint64_t available_space(Server *s, bool verbose) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering char ids[33];
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering _cleanup_free_ char *p = NULL;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering sd_id128_t machine;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering struct statvfs ss;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering uint64_t sum = 0, ss_avail = 0, avail = 0;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering int r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering _cleanup_closedir_ DIR *d = NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering usec_t ts;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering const char *f;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering JournalMetrics *m;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering ts = now(CLOCK_MONOTONIC);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering && !verbose)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return s->cached_available_space;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering r = sd_id128_get_machine(&machine);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return 0;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (s->system_journal) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering f = "/var/log/journal/";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering m = &s->system_metrics;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering } else {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering f = "/run/log/journal/";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering m = &s->runtime_metrics;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(m);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering p = strappend(f, sd_id128_to_string(machine, ids));
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (!p)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering d = opendir(p);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (!d)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (fstatvfs(dirfd(d), &ss) < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen for (;;) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct stat st;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct dirent *de;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen errno = 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering de = readdir(d);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!de && errno != 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!de)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!endswith(de->d_name, ".journal") &&
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering !endswith(de->d_name, ".journal~"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering continue;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (!S_ISREG(st.st_mode))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering sum += (uint64_t) st.st_blocks * 512UL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering ss_avail = ss.f_bsize * ss.f_bavail;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering /* If we reached a high mark, we will always allow this much
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering * again, unless usage goes above max_use. This watermark
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering * value is cached so that we don't give up space on pressure,
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering * but hover below the maximum usage. */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (m->use < sum)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering m->use = sum;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering avail = LESS_BY(ss_avail, m->keep_free);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering s->cached_available_space = LESS_BY(MIN(m->max_use, avail), sum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering s->cached_available_space_timestamp = ts;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering if (verbose) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char fb1[FORMAT_BYTES_MAX], fb2[FORMAT_BYTES_MAX], fb3[FORMAT_BYTES_MAX],
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen fb4[FORMAT_BYTES_MAX], fb5[FORMAT_BYTES_MAX];
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering server_driver_message(s, SD_MESSAGE_JOURNAL_USAGE,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "%s journal is using %s (max allowed %s, "
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "trying to leave %s free of %s available → current limit %s).",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering s->system_journal ? "Permanent" : "Runtime",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering format_bytes(fb1, sizeof(fb1), sum),
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering format_bytes(fb2, sizeof(fb2), m->max_use),
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering format_bytes(fb3, sizeof(fb3), m->keep_free),
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering format_bytes(fb4, sizeof(fb4), ss_avail),
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering format_bytes(fb5, sizeof(fb5), s->cached_available_space + sum));
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return s->cached_available_space;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringvoid server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering int r;
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen#ifdef HAVE_ACL
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering acl_t acl;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering acl_entry_t entry;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering acl_permset_t permset;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(f);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = fchmod(f->fd, 0640);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering log_warning_errno(r, "Failed to fix access mode on %s, ignoring: %m", f->path);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering#ifdef HAVE_ACL
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (uid <= SYSTEM_UID_MAX)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering acl = acl_get_fd(f->fd);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!acl) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = acl_find_uid(acl, uid, &entry);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r <= 0) {
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (acl_create_entry(&acl, &entry) < 0 ||
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering acl_set_tag_type(entry, ACL_USER) < 0 ||
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering acl_set_qualifier(entry, &uid) < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering goto finish;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering /* We do not recalculate the mask unconditionally here,
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * so that the fchmod() mask above stays intact. */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (acl_get_permset(entry, &permset) < 0 ||
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering acl_add_perm(permset, ACL_READ) < 0 ||
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering calc_acl_mask_if_needed(&acl) < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto finish;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (acl_set_fd(f->fd, acl) < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringfinish:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering acl_free(acl);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering#endif
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic JournalFile* find_journal(Server *s, uid_t uid) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering _cleanup_free_ char *p = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering int r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering JournalFile *f;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sd_id128_t machine;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering assert(s);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* We split up user logs only on /var, not on /run. If the
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * runtime file is open, we write to it exclusively, in order
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * to guarantee proper order as soon as we flush /run to
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * /var and close the runtime file. */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (s->runtime_journal)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return s->runtime_journal;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (uid <= SYSTEM_UID_MAX)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return s->system_journal;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = sd_id128_get_machine(&machine);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (r < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return s->system_journal;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering f = ordered_hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (f)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return f;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/user-"UID_FMT".journal",
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering SD_ID128_FORMAT_VAL(machine), uid) < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return s->system_journal;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering while (ordered_hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* Too many open? Then let's close one */
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering f = ordered_hashmap_steal_first(s->user_journals);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering assert(f);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering journal_file_close(f);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &f);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (r < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return s->system_journal;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering server_fix_perms(s, f, uid);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = ordered_hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (r < 0) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering journal_file_close(f);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering return s->system_journal;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return f;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int do_rotate(Server *s, JournalFile **f, const char* name,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering bool seal, uint32_t uid) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering int r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(s);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!*f)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return -EINVAL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = journal_file_rotate(f, s->compress, seal);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (r < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (*f)
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering log_error_errno(r, "Failed to rotate %s: %m",
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering (*f)->path);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering else
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_error_errno(r, "Failed to create new %s journal: %m",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering name);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering else
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering server_fix_perms(s, *f, uid);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering return r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
8937e7b68940d0fa0d0aab90eb7425fa7dccebc9Lennart Poetteringvoid server_rotate(Server *s) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering JournalFile *f;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering void *k;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering Iterator i;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering int r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_debug("Rotating...");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering do_rotate(s, &s->runtime_journal, "runtime", false, 0);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering do_rotate(s, &s->system_journal, "system", s->seal, 0);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering ORDERED_HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering r = do_rotate(s, &f, "user", s->seal, PTR_TO_UINT32(k));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r >= 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen ordered_hashmap_replace(s->user_journals, k, f);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering else if (!f)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Old file has been closed and deallocated */
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering ordered_hashmap_remove(s->user_journals, k);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringvoid server_sync(Server *s) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering JournalFile *f;
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering void *k;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering Iterator i;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering int r;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (s->system_journal) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = journal_file_set_offline(s->system_journal);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering log_error_errno(r, "Failed to sync system journal: %m");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering ORDERED_HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering r = journal_file_set_offline(f);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (r < 0)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error_errno(r, "Failed to sync user journal: %m");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (s->sync_event_source) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_OFF);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error_errno(r, "Failed to disable sync timer source: %m");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering s->sync_scheduled = false;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering}
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poetteringstatic void do_vacuum(Server *s, char *ids, JournalFile *f, const char* path,
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering JournalMetrics *metrics) {
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering char *p;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering int r;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (!f)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering p = strappenda(path, ids);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = journal_directory_vacuum(p, metrics->max_use, s->max_retention_usec, &s->oldest_file_usec, false);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0 && r != -ENOENT)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error_errno(r, "Failed to vacuum %s: %m", p);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering}
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringvoid server_vacuum(Server *s) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering char ids[33];
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering sd_id128_t machine;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_debug("Vacuuming...");
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering s->oldest_file_usec = 0;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_id128_get_machine(&machine);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_error_errno(r, "Failed to get machine ID: %m");
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering sd_id128_to_string(machine, ids);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering do_vacuum(s, ids, s->system_journal, "/var/log/journal/", &s->system_metrics);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering do_vacuum(s, ids, s->runtime_journal, "/run/log/journal/", &s->runtime_metrics);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering s->cached_available_space_timestamp = 0;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic void server_cache_machine_id(Server *s) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering sd_id128_t id;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering assert(s);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_id128_get_machine(&id);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering sd_id128_to_string(id, stpcpy(s->machine_id_field, "_MACHINE_ID="));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void server_cache_boot_id(Server *s) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering sd_id128_t id;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(s);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = sd_id128_get_boot(&id);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r < 0)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering sd_id128_to_string(id, stpcpy(s->boot_id_field, "_BOOT_ID="));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void server_cache_hostname(Server *s) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering _cleanup_free_ char *t = NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering char *x;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(s);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering t = gethostname_malloc();
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!t)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering x = strappend("_HOSTNAME=", t);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!x)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering free(s->hostname_field);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering s->hostname_field = x;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringbool shall_try_append_again(JournalFile *f, int r) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* -E2BIG Hit configured limit
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -EFBIG Hit fs limit
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -EDQUOT Quota limit hit
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -ENOSPC Disk full
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -EHOSTDOWN Other machine
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -EBUSY Unclean shutdown
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -EPROTONOSUPPORT Unsupported feature
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -EBADMSG Corrupted
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -ENODATA Truncated
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering -ESHUTDOWN Already archived */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r == -E2BIG || r == -EFBIG || r == -EDQUOT || r == -ENOSPC)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_debug("%s: Allocation limit reached, rotating.", f->path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering else if (r == -EHOSTDOWN)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_info("%s: Journal file from other machine, rotating.", f->path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering else if (r == -EBUSY)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_info("%s: Unclean shutdown, rotating.", f->path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering else if (r == -EPROTONOSUPPORT)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_info("%s: Unsupported feature, rotating.", f->path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering else if (r == -EBADMSG || r == -ENODATA || r == ESHUTDOWN)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_warning("%s: Journal file corrupted, rotating.", f->path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering else
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return false;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringstatic void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n, int priority) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering JournalFile *f;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering bool vacuumed = false;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering int r;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(s);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(iovec);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(n > 0);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering f = find_journal(s, uid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!f)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (journal_file_rotate_suggested(f, s->max_file_usec)) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers log_debug("%s: Journal header limits reached or header out-of-date, rotating.", f->path);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering server_rotate(s);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers server_vacuum(s);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering vacuumed = true;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering f = find_journal(s, uid);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (!f)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r >= 0) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering server_schedule_sync(s, priority);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (vacuumed || !shall_try_append_again(f, r)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size_t size = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (i = 0; i < n; i++)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size += iovec[i].iov_len;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(r, "Failed to write entry (%d items, %zu bytes), ignoring: %m", n, size);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering server_rotate(s);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering server_vacuum(s);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering f = find_journal(s, uid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!f)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("Retrying write.");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size_t size = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (i = 0; i < n; i++)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size += iovec[i].iov_len;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(r, "Failed to write entry (%d items, %zu bytes) despite vacuuming, ignoring: %m", n, size);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering server_schedule_sync(s, priority);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void dispatch_message_real(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Server *s,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct iovec *iovec, unsigned n, unsigned m,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const struct ucred *ucred,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const struct timeval *tv,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *label, size_t label_len,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *unit_id,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int priority,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering pid_t object_pid) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char pid[sizeof("_PID=") + DECIMAL_STR_MAX(pid_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering uid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering gid[sizeof("_GID=") + DECIMAL_STR_MAX(gid_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering owner_uid[sizeof("_SYSTEMD_OWNER_UID=") + DECIMAL_STR_MAX(uid_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering source_time[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering o_uid[sizeof("OBJECT_UID=") + DECIMAL_STR_MAX(uid_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering o_gid[sizeof("OBJECT_GID=") + DECIMAL_STR_MAX(gid_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering o_owner_uid[sizeof("OBJECT_SYSTEMD_OWNER_UID=") + DECIMAL_STR_MAX(uid_t)];
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering uid_t object_uid;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering gid_t object_gid;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering char *x;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering int r;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering char *t, *c;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering uid_t realuid = 0, owner = 0, journal_uid;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering bool owner_valid = false;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#ifdef HAVE_AUDIT
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering char audit_session[sizeof("_AUDIT_SESSION=") + DECIMAL_STR_MAX(uint32_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering audit_loginuid[sizeof("_AUDIT_LOGINUID=") + DECIMAL_STR_MAX(uid_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering o_audit_session[sizeof("OBJECT_AUDIT_SESSION=") + DECIMAL_STR_MAX(uint32_t)],
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering o_audit_loginuid[sizeof("OBJECT_AUDIT_LOGINUID=") + DECIMAL_STR_MAX(uid_t)];
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering uint32_t audit;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering uid_t loginuid;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#endif
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(s);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(iovec);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(n > 0);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(n + N_IOVEC_META_FIELDS + (object_pid ? N_IOVEC_OBJECT_FIELDS : 0) <= m);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (ucred) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering realuid = ucred->uid;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(pid, "_PID="PID_FMT, ucred->pid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering IOVEC_SET_STRING(iovec[n++], pid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(uid, "_UID="UID_FMT, ucred->uid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering IOVEC_SET_STRING(iovec[n++], uid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(gid, "_GID="GID_FMT, ucred->gid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering IOVEC_SET_STRING(iovec[n++], gid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = get_process_comm(ucred->pid, &t);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r >= 0) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering x = strappenda("_COMM=", t);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering free(t);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = get_process_exe(ucred->pid, &t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering x = strappenda("_EXE=", t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering free(t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering IOVEC_SET_STRING(iovec[n++], x);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = get_process_cmdline(ucred->pid, 0, false, &t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r >= 0) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering x = strappenda("_CMDLINE=", t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering free(t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering IOVEC_SET_STRING(iovec[n++], x);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek r = get_process_capeff(ucred->pid, &t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r >= 0) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering x = strappenda("_CAP_EFFECTIVE=", t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering free(t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering IOVEC_SET_STRING(iovec[n++], x);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#ifdef HAVE_AUDIT
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = audit_session_from_pid(ucred->pid, &audit);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering sprintf(audit_session, "_AUDIT_SESSION=%"PRIu32, audit);
a6c616024db23fef34152c1432892824a07799ccLennart Poettering IOVEC_SET_STRING(iovec[n++], audit_session);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = audit_loginuid_from_pid(ucred->pid, &loginuid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r >= 0) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(audit_loginuid, "_AUDIT_LOGINUID="UID_FMT, loginuid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering IOVEC_SET_STRING(iovec[n++], audit_loginuid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#endif
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &c);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r >= 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers char *session = NULL;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
a6c616024db23fef34152c1432892824a07799ccLennart Poettering x = strappenda("_SYSTEMD_CGROUP=", c);
a6c616024db23fef34152c1432892824a07799ccLennart Poettering IOVEC_SET_STRING(iovec[n++], x);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = cg_path_get_session(c, &t);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (r >= 0) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering session = strappenda("_SYSTEMD_SESSION=", t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering IOVEC_SET_STRING(iovec[n++], session);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (cg_path_get_owner_uid(c, &owner) >= 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers owner_valid = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers sprintf(owner_uid, "_SYSTEMD_OWNER_UID="UID_FMT, owner);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt IOVEC_SET_STRING(iovec[n++], owner_uid);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (cg_path_get_unit(c, &t) >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering x = strappenda("_SYSTEMD_UNIT=", t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else if (unit_id && !session) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering x = strappenda("_SYSTEMD_UNIT=", unit_id);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers IOVEC_SET_STRING(iovec[n++], x);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (cg_path_get_user_unit(c, &t) >= 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers x = strappenda("_SYSTEMD_USER_UNIT=", t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering free(t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else if (unit_id && session) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers x = strappenda("_SYSTEMD_USER_UNIT=", unit_id);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (cg_path_get_slice(c, &t) >= 0) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering x = strappenda("_SYSTEMD_SLICE=", t);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering free(t);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers free(c);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (unit_id) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers x = strappenda("_SYSTEMD_UNIT=", unit_id);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen IOVEC_SET_STRING(iovec[n++], x);
27e72d6b22890ba4a8cbc05c49667cd1cccf1461Simon Peeters }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt#ifdef HAVE_SELINUX
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (mac_selinux_use()) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (label) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering x = alloca(strlen("_SELINUX_CONTEXT=") + label_len + 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen } else {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering security_context_t con;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (getpidcon(ucred->pid, &con) >= 0) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering x = strappenda("_SELINUX_CONTEXT=", con);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering freecon(con);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert(n <= m);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (object_pid) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = get_process_uid(object_pid, &object_uid);
8c841f21f5042b11acc91cc1b039cb162cbbe8f4Djalal Harouni if (r >= 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers sprintf(o_uid, "OBJECT_UID="UID_FMT, object_uid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering IOVEC_SET_STRING(iovec[n++], o_uid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = get_process_gid(object_pid, &object_gid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r >= 0) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sprintf(o_gid, "OBJECT_GID="GID_FMT, object_gid);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen IOVEC_SET_STRING(iovec[n++], o_gid);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = get_process_comm(object_pid, &t);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r >= 0) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen x = strappenda("OBJECT_COMM=", t);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen free(t);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = get_process_exe(object_pid, &t);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r >= 0) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen x = strappenda("OBJECT_EXE=", t);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen free(t);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering IOVEC_SET_STRING(iovec[n++], x);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers r = get_process_cmdline(object_pid, 0, false, &t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r >= 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers x = strappenda("OBJECT_CMDLINE=", t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering#ifdef HAVE_AUDIT
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = audit_session_from_pid(object_pid, &audit);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering sprintf(o_audit_session, "OBJECT_AUDIT_SESSION=%"PRIu32, audit);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], o_audit_session);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = audit_loginuid_from_pid(object_pid, &loginuid);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r >= 0) {
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering sprintf(o_audit_loginuid, "OBJECT_AUDIT_LOGINUID="UID_FMT, loginuid);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering IOVEC_SET_STRING(iovec[n++], o_audit_loginuid);
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering#endif
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = cg_pid_get_path_shifted(object_pid, s->cgroup_root, &c);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_CGROUP=", c);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = cg_path_get_session(c, &t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_SESSION=", t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (cg_path_get_owner_uid(c, &owner) >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering sprintf(o_owner_uid, "OBJECT_SYSTEMD_OWNER_UID="UID_FMT, owner);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], o_owner_uid);
9a14fb6285bdb089d4fc195410de3362cb4f586fThomas Hindoe Paaboel Andersen }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (cg_path_get_unit(c, &t) >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_UNIT=", t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (cg_path_get_user_unit(c, &t) >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_USER_UNIT=", t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], x);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(c);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(n <= m);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (tv) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering sprintf(source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu", (unsigned long long) timeval_load(tv));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], source_time);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering /* Note that strictly speaking storing the boot id here is
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering * redundant since the entry includes this in-line
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering * anyway. However, we need this indexed, too. */
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (!isempty(s->boot_id_field))
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering IOVEC_SET_STRING(iovec[n++], s->boot_id_field);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering if (!isempty(s->machine_id_field))
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering IOVEC_SET_STRING(iovec[n++], s->machine_id_field);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (!isempty(s->hostname_field))
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering IOVEC_SET_STRING(iovec[n++], s->hostname_field);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering assert(n <= m);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (s->split_mode == SPLIT_UID && realuid > 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Split up strictly by any UID */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering journal_uid = realuid;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering else if (s->split_mode == SPLIT_LOGIN && realuid > 0 && owner_valid && owner > 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Split up by login UIDs. We do this only if the
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering * realuid is not root, in order not to accidentally
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering * leak privileged information to the user that is
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering * logged by a privileged process that is part of an
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering * unprivileged session.*/
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering journal_uid = owner;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering else
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering journal_uid = 0;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering write_to_journal(s, journal_uid, iovec, n, priority);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering}
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringvoid server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char mid[11 + 32 + 1];
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char buffer[16 + LINE_MAX + 1];
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 4];
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int n = 0;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering va_list ap;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering struct ucred ucred = {};
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(s);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(format);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], "PRIORITY=6");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering memcpy(buffer, "MESSAGE=", 8);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering va_start(ap, format);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering va_end(ap);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char_array_0(buffer);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], buffer);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering snprintf(mid, sizeof(mid), LOG_MESSAGE_ID(message_id));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char_array_0(mid);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], mid);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering ucred.pid = getpid();
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering ucred.uid = getuid();
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering ucred.gid = getgid();
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, LOG_INFO, 0);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering}
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringvoid server_dispatch_message(
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering Server *s,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering struct iovec *iovec, unsigned n, unsigned m,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering const struct ucred *ucred,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering const struct timeval *tv,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering const char *label, size_t label_len,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering const char *unit_id,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int priority,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering pid_t object_pid) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int rl, r;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering _cleanup_free_ char *path = NULL;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char *c;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(s);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(iovec || n == 0);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (n == 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (LOG_PRI(priority) > s->max_level_store)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Stop early in case the information will not be stored
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * in a journal. */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (s->storage == STORAGE_NONE)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (!ucred)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering goto finish;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r < 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering goto finish;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* example: /user/lennart/3/foobar
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * /system/dbus.service/foobar
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering *
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * So let's cut of everything past the third /, since that is
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * where user directories start */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering c = strchr(path, '/');
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (c) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering c = strchr(c+1, '/');
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (c) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering c = strchr(c+1, '/');
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (c)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering *c = 0;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering rl = journal_rate_limit_test(s->rate_limit, path,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering priority & LOG_PRIMASK, available_space(s, false));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (rl == 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Write a suppression message if we suppressed something */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (rl > 1)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering server_driver_message(s, SD_MESSAGE_JOURNAL_DROPPED,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering "Suppressed %u messages from %s", rl - 1, path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringfinish:
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, priority, object_pid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int system_journal_open(Server *s, bool flush_requested) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering char *fn;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sd_id128_t machine;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering char ids[33];
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_id128_get_machine(&machine);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to get machine id: %m");
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering sd_id128_to_string(machine, ids);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!s->system_journal &&
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering (flush_requested
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering || access("/run/systemd/journal/flushed", F_OK) >= 0)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* If in auto mode: first try to create the machine
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * path, but not the prefix.
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering *
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * If in persistent mode: create /var/log/journal and
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * the machine path */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (s->storage == STORAGE_PERSISTENT)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering (void) mkdir("/var/log/journal/", 0755);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen fn = strappenda("/var/log/journal/", ids);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen (void) mkdir(fn, 0755);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen fn = strappenda(fn, "/system.journal");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r >= 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering server_fix_perms(s, s->system_journal, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (r < 0) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (r != -ENOENT && r != -EROFS)
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering log_warning_errno(r, "Failed to open system journal: %m");
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = 0;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (!s->runtime_journal &&
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering (s->storage != STORAGE_NONE)) {
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (!fn)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -ENOMEM;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (s->system_journal) {
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering /* Try to open the runtime journal, but only
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * if it already exists, so that we can flush
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering * it into the system journal */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering free(fn);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (r < 0) {
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering if (r != -ENOENT)
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering log_warning_errno(r, "Failed to open runtime journal: %m");
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering r = 0;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering }
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering } else {
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering /* OK, we really need the runtime journal, so create
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering * it if necessary. */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering (void) mkdir("/run/log", 0755);
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering (void) mkdir("/run/log/journal", 0755);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering (void) mkdir_parents(fn, 0750);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering free(fn);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering if (r < 0)
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering return log_error_errno(r, "Failed to open runtime journal: %m");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (s->runtime_journal)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_fix_perms(s, s->runtime_journal, 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering available_space(s, true);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering}
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringint server_flush_to_var(Server *s) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering sd_id128_t machine;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering sd_journal *j = NULL;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering char ts[FORMAT_TIMESPAN_MAX];
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering usec_t start;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering unsigned n = 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering int r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (s->storage != STORAGE_AUTO &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering s->storage != STORAGE_PERSISTENT)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (!s->runtime_journal)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering system_journal_open(s, true);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (!s->system_journal)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_debug("Flushing to /var...");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering start = now(CLOCK_MONOTONIC);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_id128_get_machine(&machine);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return log_error_errno(r, "Failed to read runtime journal: %m");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering sd_journal_set_data_threshold(j, 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering SD_JOURNAL_FOREACH(j) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering Object *o = NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering JournalFile *f;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering f = j->current_file;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering assert(f && f->current_offset > 0);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering n++;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (r < 0) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_error_errno(r, "Can't read entry: %m");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering goto finish;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (r >= 0)
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering continue;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (!shall_try_append_again(s->system_journal, r)) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_error_errno(r, "Can't write entry: %m");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering goto finish;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering }
bb4a228207815df88cdf68acd9e46ec19e0d3e30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering server_rotate(s);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering server_vacuum(s);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
bb4a228207815df88cdf68acd9e46ec19e0d3e30Lennart Poettering if (!s->system_journal) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_notice("Didn't flush runtime journal since rotation of system journal wasn't successful.");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering r = -EIO;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering goto finish;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_debug("Retrying write.");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (r < 0) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_error_errno(r, "Can't write entry: %m");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering goto finish;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering }
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering }
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poetteringfinish:
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering journal_file_post_change(s->system_journal);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering journal_file_close(s->runtime_journal);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering s->runtime_journal = NULL;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (r >= 0)
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering rm_rf("/run/log/journal", false, true, false);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering sd_journal_close(j);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering server_driver_message(s, SD_ID128_NULL, "Time spent on flushing to /var is %s for %u entries.", format_timespan(ts, sizeof(ts), now(CLOCK_MONOTONIC) - start, 0), n);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering return r;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering}
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poetteringint process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering Server *s = userdata;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering assert(s);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering assert(fd == s->native_fd || fd == s->syslog_fd || fd == s->audit_fd);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (revents != EPOLLIN) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_error("Got invalid event from epoll for datagram fd: %"PRIx32, revents);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering return -EIO;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering }
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering for (;;) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering struct ucred *ucred = NULL;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering struct timeval *tv = NULL;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering struct cmsghdr *cmsg;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering char *label = NULL;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering size_t label_len = 0;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering struct iovec iovec;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering union {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering struct cmsghdr cmsghdr;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering /* We use NAME_MAX space for the SELinux label
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering * here. The kernel currently enforces no
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering * limit, but according to suggestions from
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering * the SELinux people this will change and it
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering * will probably be identical to NAME_MAX. For
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering * now we use that, but this should be updated
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering * one day when the final limit is known.*/
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering CMSG_SPACE(sizeof(struct timeval)) +
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering CMSG_SPACE(sizeof(int)) + /* fd */
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering CMSG_SPACE(NAME_MAX)]; /* selinux label */
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering } control = {};
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering union sockaddr_union sa = {};
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering struct msghdr msghdr = {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering .msg_iov = &iovec,
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering .msg_iovlen = 1,
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering .msg_control = &control,
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering .msg_controllen = sizeof(control),
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering .msg_name = &sa,
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering .msg_namelen = sizeof(sa),
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering };
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering ssize_t n;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering int *fds = NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering unsigned n_fds = 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering int v = 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering size_t m;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering /* Try to get the right size, if we can. (Not all
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * sockets support SIOCINQ, hence we just try, but
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering * don't rely on it. */
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering (void) ioctl(fd, SIOCINQ, &v);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering /* Fix it up, if it is too small. We use the same fixed value as auditd here. Awful!*/
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering m = PAGE_ALIGN(MAX3((size_t) v + 1,
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering (size_t) LINE_MAX,
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering ALIGN(sizeof(struct nlmsghdr)) + ALIGN((size_t) MAX_AUDIT_MESSAGE_LENGTH)) + 1);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (!GREEDY_REALLOC(s->buffer, s->buffer_size, m))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return log_oom();
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering iovec.iov_base = s->buffer;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering iovec.iov_len = s->buffer_size - 1; /* Leave room for trailing NUL we add later */
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering n = recvmsg(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (n < 0) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (errno == EINTR || errno == EAGAIN)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_error("recvmsg() failed: %m");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return -errno;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (cmsg->cmsg_level == SOL_SOCKET &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_type == SCM_CREDENTIALS &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering ucred = (struct ucred*) CMSG_DATA(cmsg);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (cmsg->cmsg_level == SOL_SOCKET &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_type == SCM_SECURITY) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering label = (char*) CMSG_DATA(cmsg);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering label_len = cmsg->cmsg_len - CMSG_LEN(0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering } else if (cmsg->cmsg_level == SOL_SOCKET &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_type == SO_TIMESTAMP &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering tv = (struct timeval*) CMSG_DATA(cmsg);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (cmsg->cmsg_level == SOL_SOCKET &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_type == SCM_RIGHTS) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering fds = (int*) CMSG_DATA(cmsg);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering /* And a trailing NUL, just in case */
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering s->buffer[n] = 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (fd == s->syslog_fd) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (n > 0 && n_fds == 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (n_fds > 0)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_warning("Got file descriptors via syslog socket. Ignoring.");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering } else if (fd == s->native_fd) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (n > 0 && n_fds == 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_process_native_message(s, s->buffer, n, ucred, tv, label, label_len);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (n == 0 && n_fds == 1)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_process_native_file(s, fds[0], ucred, tv, label, label_len);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (n_fds > 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_warning("Got too many file descriptors via native socket. Ignoring.");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering } else {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert(fd == s->audit_fd);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (n > 0 && n_fds == 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_process_audit_message(s, s->buffer, n, ucred, &sa, msghdr.msg_namelen);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (n_fds > 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_warning("Got file descriptors via audit socket. Ignoring.");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering close_many(fds, n_fds);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering}
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering Server *s = userdata;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_info("Received request to flush runtime journal from PID %"PRIu32, si->ssi_pid);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_flush_to_var(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_sync(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_vacuum(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering touch("/run/systemd/journal/flushed");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering}
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int dispatch_sigusr2(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering Server *s = userdata;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_info("Received request to rotate journal from PID %"PRIu32, si->ssi_pid);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_rotate(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_vacuum(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering}
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering Server *s = userdata;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_received_signal(LOG_INFO, si);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering sd_event_exit(s->event, 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering}
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int setup_signals(Server *s) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering sigset_t mask;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering int r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert(s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert_se(sigemptyset(&mask) == 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -1);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigusr1_event_source, SIGUSR1, dispatch_sigusr1, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigusr2_event_source, SIGUSR2, dispatch_sigusr2, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigterm_event_source, SIGTERM, dispatch_sigterm, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigint_event_source, SIGINT, dispatch_sigterm, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering}
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int server_parse_proc_cmdline(Server *s) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering _cleanup_free_ char *line = NULL;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering const char *w, *state;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering size_t l;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering int r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = proc_cmdline(&line);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering FOREACH_WORD_QUOTED(w, l, line, state) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering _cleanup_free_ char *word;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering word = strndup(w, l);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (!word)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return -ENOMEM;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (startswith(word, "systemd.journald.forward_to_syslog=")) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = parse_boolean(word + 35);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering s->forward_to_syslog = r;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering } else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering r = parse_boolean(word + 33);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (r < 0)
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering else
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering s->forward_to_kmsg = r;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering } else if (startswith(word, "systemd.journald.forward_to_console=")) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering r = parse_boolean(word + 36);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (r < 0)
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering else
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering s->forward_to_console = r;
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering } else if (startswith(word, "systemd.journald.forward_to_wall=")) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering r = parse_boolean(word + 33);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (r < 0)
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_warning("Failed to parse forward to wall switch %s. Ignoring.", word + 33);
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering else
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering s->forward_to_wall = r;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering } else if (startswith(word, "systemd.journald"))
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_warning("Invalid systemd.journald parameter. Ignoring.");
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering }
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering /* do not warn about state here, since probably systemd already did */
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return 0;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poetteringstatic int server_parse_config_file(Server *s) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering assert(s);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering return config_parse(NULL, "/etc/systemd/journald.conf", NULL,
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering "Journal\0",
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering config_item_perf_lookup, journald_gperf_lookup,
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering false, false, true, s);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering}
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poetteringstatic int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering Server *s = userdata;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering assert(s);
bf441e3d9371a7e5aa1def66cfc40f0118884644Lennart Poettering
de33fc625725d199629ed074d6278504deb23debLennart Poettering server_sync(s);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering return 0;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering}
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint server_schedule_sync(Server *s, int priority) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering int r;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering assert(s);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (priority <= LOG_CRIT) {
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering /* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering server_sync(s);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return 0;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt }
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (s->sync_scheduled)
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return 0;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (s->sync_interval_usec > 0) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering usec_t when;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering r = sd_event_now(s->event, CLOCK_MONOTONIC, &when);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (r < 0)
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return r;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering when += s->sync_interval_usec;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (!s->sync_event_source) {
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering r = sd_event_add_time(
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering s->event,
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering &s->sync_event_source,
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering CLOCK_MONOTONIC,
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering when, 0,
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering server_dispatch_sync, s);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering if (r < 0)
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering return r;
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering r = sd_event_source_set_priority(s->sync_event_source, SD_EVENT_PRIORITY_IMPORTANT);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering } else {
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering r = sd_event_source_set_time(s->sync_event_source, when);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering if (r < 0)
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering return r;
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_ONESHOT);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering }
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering if (r < 0)
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering return r;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering s->sync_scheduled = true;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering return 0;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering}
ee451d766a64117a41ec36dd71e61683c9d9b83cLennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringstatic int dispatch_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering Server *s = userdata;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering assert(s);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering server_cache_hostname(s);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return 0;
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering}
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poetteringstatic int server_open_hostname(Server *s) {
9b15b7846d4de01bb5d9700a24077787e984e8abLennart Poettering int r;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert(s);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering s->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (s->hostname_fd < 0) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt log_error("Failed to open /proc/sys/kernel/hostname: %m");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return -errno;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering }
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering r = sd_event_add_io(s->event, &s->hostname_event_source, s->hostname_fd, 0, dispatch_hostname_change, s);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering if (r < 0) {
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering /* kernels prior to 3.2 don't support polling this file. Ignore
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering * the failure. */
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering if (r == -EPERM) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_warning("Failed to register hostname fd in event loop: %s. Ignoring.",
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering strerror(-r));
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering s->hostname_fd = safe_close(s->hostname_fd);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return 0;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering return log_error_errno(r, "Failed to register hostname fd in event loop: %m");
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = sd_event_source_set_priority(s->hostname_event_source, SD_EVENT_PRIORITY_IMPORTANT-10);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if (r < 0)
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering return log_error_errno(r, "Failed to adjust priority of host name event source: %m");
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return 0;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering}
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poetteringint server_init(Server *s) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering int n, r, fd;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering assert(s);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering zero(*s);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->syslog_fd = s->native_fd = s->stdout_fd = s->dev_kmsg_fd = s->audit_fd = s->hostname_fd = -1;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->compress = true;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->seal = true;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->sync_interval_usec = DEFAULT_SYNC_INTERVAL_USEC;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->sync_scheduled = false;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->forward_to_wall = true;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->max_file_usec = DEFAULT_MAX_FILE_USEC;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->max_level_store = LOG_DEBUG;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->max_level_syslog = LOG_DEBUG;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->max_level_kmsg = LOG_NOTICE;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->max_level_console = LOG_INFO;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->max_level_wall = LOG_EMERG;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering server_parse_config_file(s);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering server_parse_proc_cmdline(s);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (!!s->rate_limit_interval ^ !!s->rate_limit_burst) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_debug("Setting both rate limit interval and burst from "USEC_FMT",%u to 0,0",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->rate_limit_interval, s->rate_limit_burst);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->rate_limit_interval = s->rate_limit_burst = 0;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering mkdir_p("/run/systemd/journal", 0755);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->user_journals = ordered_hashmap_new(NULL);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (!s->user_journals)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return log_oom();
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->mmap = mmap_cache_new();
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (!s->mmap)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return log_oom();
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = sd_event_default(&s->event);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return log_error_errno(r, "Failed to create event loop: %m");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering sd_event_set_watchdog(s->event, true);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering n = sd_listen_fds(true);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (n < 0)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (s->native_fd >= 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Too many native sockets passed.");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return -EINVAL;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->native_fd = fd;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (s->stdout_fd >= 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Too many stdout sockets passed.");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return -EINVAL;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->stdout_fd = fd;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0 ||
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/dev-log", 0) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (s->syslog_fd >= 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Too many /dev/log sockets passed.");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return -EINVAL;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering }
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->syslog_fd = fd;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering } else if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (s->audit_fd >= 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Too many audit sockets passed.");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return -EINVAL;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering s->audit_fd = fd;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering } else
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Unknown socket passed as file descriptor %d, ignoring.", fd);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = server_open_syslog_socket(s);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = server_open_native_socket(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = server_open_stdout_socket(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return r;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = server_open_dev_kmsg(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = server_open_audit(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = server_open_kernel_seqnum(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = server_open_hostname(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = setup_signals(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering s->udev = udev_new();
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (!s->udev)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return -ENOMEM;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (!s->rate_limit)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return -ENOMEM;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = cg_get_root_path(&s->cgroup_root);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering server_cache_hostname(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering server_cache_boot_id(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering server_cache_machine_id(s);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = system_journal_open(s, false);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return 0;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering}
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poetteringvoid server_maybe_append_tags(Server *s) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering#ifdef HAVE_GCRYPT
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering JournalFile *f;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering Iterator i;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering usec_t n;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering n = now(CLOCK_REALTIME);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (s->system_journal)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering journal_file_maybe_append_tag(s->system_journal, n);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering ORDERED_HASHMAP_FOREACH(f, s->user_journals, i)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering journal_file_maybe_append_tag(f, n);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering#endif
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering}
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poetteringvoid server_done(Server *s) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering JournalFile *f;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering assert(s);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering while (s->stdout_streams)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering stdout_stream_free(s->stdout_streams);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (s->system_journal)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering journal_file_close(s->system_journal);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (s->runtime_journal)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering journal_file_close(s->runtime_journal);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering while ((f = ordered_hashmap_steal_first(s->user_journals)))
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering journal_file_close(f);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering ordered_hashmap_free(s->user_journals);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->syslog_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->native_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->stdout_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->dev_kmsg_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->audit_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->sync_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->sigusr1_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->sigusr2_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->sigterm_event_source);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering sd_event_source_unref(s->sigint_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_source_unref(s->hostname_event_source);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_event_unref(s->event);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering safe_close(s->syslog_fd);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering safe_close(s->native_fd);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering safe_close(s->stdout_fd);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering safe_close(s->dev_kmsg_fd);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering safe_close(s->audit_fd);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering safe_close(s->hostname_fd);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (s->rate_limit)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering journal_rate_limit_free(s->rate_limit);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (s->kernel_seqnum)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering munmap(s->kernel_seqnum, sizeof(uint64_t));
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering free(s->buffer);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering free(s->tty_path);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering free(s->cgroup_root);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering free(s->hostname_field);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (s->mmap)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering mmap_cache_unref(s->mmap);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (s->udev)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering udev_unref(s->udev);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering}
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering