journald-server.c revision 23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2011 Lennart 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 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 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/>.
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#define DEFAULT_SYNC_INTERVAL_USEC (5*USEC_PER_MINUTE)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering#define DEFAULT_RATE_LIMIT_INTERVAL (30*USEC_PER_SEC)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#define DEFAULT_MAX_FILE_USEC USEC_PER_MONTH
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering#define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic const char* const storage_table[_STORAGE_MAX] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(storage, Storage);
1ee306e1248866617c96ed9f4263f375588ad838Lennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic const char* const split_mode_table[_SPLIT_MAX] = {
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 Poetteringstatic uint64_t available_space(Server *s, bool verbose) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering uint64_t sum = 0, ss_avail = 0, avail = 0;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering const char *f;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering p = strappend(f, sd_id128_to_string(machine, ids));
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
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 s->cached_available_space = LESS_BY(MIN(m->max_use, avail), sum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering s->cached_available_space_timestamp = ts;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char fb1[FORMAT_BYTES_MAX], fb2[FORMAT_BYTES_MAX], fb3[FORMAT_BYTES_MAX],
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(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 Poetteringvoid server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering log_warning_errno(r, "Failed to fix access mode on %s, ignoring: %m", f->path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (acl_create_entry(&acl, &entry) < 0 ||
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
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 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic JournalFile* find_journal(Server *s, uid_t uid) {
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. */
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering f = ordered_hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/user-"UID_FMT".journal",
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 r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &f);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = ordered_hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int do_rotate(Server *s, JournalFile **f, const char* name,
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = journal_file_rotate(f, s->compress, seal);
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering log_error_errno(r, "Failed to rotate %s: %m",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_error_errno(r, "Failed to create new %s journal: %m",
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 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 ordered_hashmap_replace(s->user_journals, k, f);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Old file has been closed and deallocated */
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering ordered_hashmap_remove(s->user_journals, k);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = journal_file_set_offline(s->system_journal);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering log_error_errno(r, "Failed to sync system journal: %m");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering ORDERED_HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error_errno(r, "Failed to sync user journal: %m");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_OFF);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error_errno(r, "Failed to disable sync timer source: %m");
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poetteringstatic void do_vacuum(Server *s, char *ids, JournalFile *f, const char* path,
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);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_error_errno(r, "Failed to get machine ID: %m");
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);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic void server_cache_machine_id(Server *s) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering sd_id128_to_string(id, stpcpy(s->machine_id_field, "_MACHINE_ID="));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void server_cache_boot_id(Server *s) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering sd_id128_to_string(id, stpcpy(s->boot_id_field, "_BOOT_ID="));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void server_cache_hostname(Server *s) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringbool shall_try_append_again(JournalFile *f, int r) {
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 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 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 Poetteringstatic void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n, int priority) {
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);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (vacuumed || !shall_try_append_again(f, r)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (i = 0; i < n; i++)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(r, "Failed to write entry (%d items, %zu bytes), ignoring: %m", n, size);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (i = 0; i < n; i++)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(r, "Failed to write entry (%d items, %zu bytes) despite vacuuming, ignoring: %m", n, size);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct iovec *iovec, unsigned n, unsigned m,
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 realuid = 0, owner = 0, journal_uid;
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)];
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(n + N_IOVEC_META_FIELDS + (object_pid ? N_IOVEC_OBJECT_FIELDS : 0) <= m);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(pid, "_PID="PID_FMT, ucred->pid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(uid, "_UID="UID_FMT, ucred->uid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(gid, "_GID="GID_FMT, ucred->gid);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = get_process_cmdline(ucred->pid, 0, false, &t);
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek r = get_process_capeff(ucred->pid, &t);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = audit_session_from_pid(ucred->pid, &audit);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering sprintf(audit_session, "_AUDIT_SESSION=%"PRIu32, audit);
a6c616024db23fef34152c1432892824a07799ccLennart Poettering IOVEC_SET_STRING(iovec[n++], audit_session);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = audit_loginuid_from_pid(ucred->pid, &loginuid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sprintf(audit_loginuid, "_AUDIT_LOGINUID="UID_FMT, loginuid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering IOVEC_SET_STRING(iovec[n++], audit_loginuid);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &c);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering session = strappenda("_SYSTEMD_SESSION=", t);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers sprintf(owner_uid, "_SYSTEMD_OWNER_UID="UID_FMT, owner);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (cg_path_get_unit(c, &t) >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering x = strappenda("_SYSTEMD_UNIT=", unit_id);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (cg_path_get_user_unit(c, &t) >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (cg_path_get_slice(c, &t) >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering x = alloca(strlen("_SELINUX_CONTEXT=") + label_len + 1);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering x = strappenda("_SELINUX_CONTEXT=", con);
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 r = get_process_gid(object_pid, &object_gid);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sprintf(o_gid, "OBJECT_GID="GID_FMT, object_gid);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r >= 0) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r >= 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers r = get_process_cmdline(object_pid, 0, false, &t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = audit_session_from_pid(object_pid, &audit);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering sprintf(o_audit_session, "OBJECT_AUDIT_SESSION=%"PRIu32, audit);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], o_audit_session);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = audit_loginuid_from_pid(object_pid, &loginuid);
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering sprintf(o_audit_loginuid, "OBJECT_AUDIT_LOGINUID="UID_FMT, loginuid);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering IOVEC_SET_STRING(iovec[n++], o_audit_loginuid);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = cg_pid_get_path_shifted(object_pid, s->cgroup_root, &c);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_CGROUP=", c);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_SESSION=", t);
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);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (cg_path_get_unit(c, &t) >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_UNIT=", t);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (cg_path_get_user_unit(c, &t) >= 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering x = strappenda("OBJECT_SYSTEMD_USER_UNIT=", t);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering sprintf(source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu", (unsigned long long) timeval_load(tv));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], source_time);
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. */
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering IOVEC_SET_STRING(iovec[n++], s->boot_id_field);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering IOVEC_SET_STRING(iovec[n++], s->machine_id_field);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering IOVEC_SET_STRING(iovec[n++], s->hostname_field);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (s->split_mode == SPLIT_UID && realuid > 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Split up strictly by any UID */
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.*/
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering write_to_journal(s, journal_uid, iovec, n, priority);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringvoid server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 4];
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], "PRIORITY=6");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering snprintf(mid, sizeof(mid), LOG_MESSAGE_ID(message_id));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, LOG_INFO, 0);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering struct iovec *iovec, unsigned n, unsigned m,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (LOG_PRI(priority) > s->max_level_store)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Stop early in case the information will not be stored
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * in a journal. */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* example: /user/lennart/3/foobar
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * So let's cut of everything past the third /, since that is
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * where user directories start */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering rl = journal_rate_limit_test(s->rate_limit, path,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering priority & LOG_PRIMASK, available_space(s, false));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Write a suppression message if we suppressed something */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering server_driver_message(s, SD_MESSAGE_JOURNAL_DROPPED,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering "Suppressed %u messages from %s", rl - 1, path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, priority, object_pid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int system_journal_open(Server *s, bool flush_requested) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to get machine id: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering || access("/run/systemd/journal/flushed", F_OK) >= 0)) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* If in auto mode: first try to create the machine
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * path, but not the prefix.
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * If in persistent mode: create /var/log/journal and
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * the machine path */
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 server_fix_perms(s, s->system_journal, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (r < 0) {
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering log_warning_errno(r, "Failed to open system journal: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
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 */
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering log_warning_errno(r, "Failed to open runtime journal: %m");
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering /* OK, we really need the runtime journal, so create
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering * it if necessary. */
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 return log_error_errno(r, "Failed to open runtime journal: %m");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_fix_perms(s, s->runtime_journal, 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering unsigned n = 0;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return log_error_errno(r, "Failed to read runtime journal: %m");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_error_errno(r, "Can't read entry: %m");
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (!shall_try_append_again(s->system_journal, r)) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_error_errno(r, "Can't write entry: %m");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_notice("Didn't flush runtime journal since rotation of system journal wasn't successful.");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_error_errno(r, "Can't write entry: %m");
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering journal_file_post_change(s->system_journal);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering rm_rf("/run/log/journal", false, true, false);
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 Poetteringint process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering assert(fd == s->native_fd || fd == s->syslog_fd || fd == s->audit_fd);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering log_error("Got invalid event from epoll for datagram fd: %"PRIx32, revents);
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(NAME_MAX)]; /* selinux label */
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 /* Fix it up, if it is too small. We use the same fixed value as auditd here. Awful!*/
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering ALIGN(sizeof(struct nlmsghdr)) + ALIGN((size_t) MAX_AUDIT_MESSAGE_LENGTH)) + 1);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (!GREEDY_REALLOC(s->buffer, s->buffer_size, m))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering iovec.iov_len = s->buffer_size - 1; /* Leave room for trailing NUL we add later */
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering n = recvmsg(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (cmsg->cmsg_level == SOL_SOCKET &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering label_len = cmsg->cmsg_len - CMSG_LEN(0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering } else if (cmsg->cmsg_level == SOL_SOCKET &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering else if (cmsg->cmsg_level == SOL_SOCKET &&
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering /* And a trailing NUL, just in case */
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (n > 0 && n_fds == 0)
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_warning("Got file descriptors via syslog socket. Ignoring.");
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 server_process_native_file(s, fds[0], ucred, tv, label, label_len);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_warning("Got too many file descriptors via native socket. Ignoring.");
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 log_warning("Got file descriptors via audit socket. Ignoring.");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_info("Received request to flush runtime journal from PID %"PRIu32, si->ssi_pid);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int dispatch_sigusr2(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_info("Received request to rotate journal from PID %"PRIu32, si->ssi_pid);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -1);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigusr1_event_source, SIGUSR1, dispatch_sigusr1, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigusr2_event_source, SIGUSR2, dispatch_sigusr2, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigterm_event_source, SIGTERM, dispatch_sigterm, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_add_signal(s->event, &s->sigint_event_source, SIGINT, dispatch_sigterm, s);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int server_parse_proc_cmdline(Server *s) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering const char *w, *state;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (startswith(word, "systemd.journald.forward_to_syslog=")) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering } else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering } else if (startswith(word, "systemd.journald.forward_to_console=")) {
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering } else if (startswith(word, "systemd.journald.forward_to_wall=")) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_warning("Failed to parse forward to wall switch %s. Ignoring.", word + 33);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering } else if (startswith(word, "systemd.journald"))
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_warning("Invalid systemd.journald parameter. Ignoring.");
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering /* do not warn about state here, since probably systemd already did */
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poetteringstatic int server_parse_config_file(Server *s) {
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering return config_parse(NULL, "/etc/systemd/journald.conf", NULL,
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering config_item_perf_lookup, journald_gperf_lookup,
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering false, false, true, s);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poetteringstatic int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringint server_schedule_sync(Server *s, int priority) {
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering /* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering r = sd_event_now(s->event, CLOCK_MONOTONIC, &when);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering r = sd_event_source_set_priority(s->sync_event_source, SD_EVENT_PRIORITY_IMPORTANT);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering r = sd_event_source_set_time(s->sync_event_source, when);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_ONESHOT);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poetteringstatic int dispatch_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poetteringstatic int server_open_hostname(Server *s) {
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering s->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt log_error("Failed to open /proc/sys/kernel/hostname: %m");
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering r = sd_event_add_io(s->event, &s->hostname_event_source, s->hostname_fd, 0, dispatch_hostname_change, s);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering /* kernels prior to 3.2 don't support polling this file. Ignore
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering * the failure. */
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_warning("Failed to register hostname fd in event loop: %s. Ignoring.",
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering s->hostname_fd = safe_close(s->hostname_fd);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering return log_error_errno(r, "Failed to register hostname fd in event loop: %m");
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = sd_event_source_set_priority(s->hostname_event_source, SD_EVENT_PRIORITY_IMPORTANT-10);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering return log_error_errno(r, "Failed to adjust priority of host name event source: %m");
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->sync_interval_usec = DEFAULT_SYNC_INTERVAL_USEC;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering s->max_file_usec = DEFAULT_MAX_FILE_USEC;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
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 s->user_journals = ordered_hashmap_new(NULL);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return log_error_errno(r, "Failed to create event loop: %m");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Too many native sockets passed.");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Too many stdout sockets passed.");
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 log_error("Too many /dev/log sockets passed.");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering } else if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Too many audit sockets passed.");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Unknown socket passed as file descriptor %d, ignoring.", fd);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poetteringvoid server_maybe_append_tags(Server *s) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering journal_file_maybe_append_tag(s->system_journal, n);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering ORDERED_HASHMAP_FOREACH(f, s->user_journals, i)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering while ((f = ordered_hashmap_steal_first(s->user_journals)))
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);