journald.c revision fe6521272ba203ec8f0d5a94f0729960b3f90525
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering This file is part of systemd.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering Copyright 2011 Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is free software; you can redistribute it and/or modify it
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering under the terms of the GNU General Public License as published by
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering the Free Software Foundation; either version 2 of the License, or
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (at your option) any later version.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is distributed in the hope that it will be useful, but
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering General Public License for more details.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering You should have received a copy of the GNU General Public License
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringtypedef struct Server {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering LIST_FIELDS(StdoutStream, stdout_stream);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poetteringstatic void fix_perms(JournalFile *f, uid_t uid) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering r = fchmod_and_fchown(f->fd, 0640, 0, 0);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (acl_create_entry(&acl, &entry) < 0 ||
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (acl_get_permset(entry, &permset) < 0 ||
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poetteringstatic JournalFile* find_journal(Server *s, uid_t uid) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering /* We split up user logs only on /var, not on /run */
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering while (hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering /* Too many open? Then let's close one */
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering f = hashmap_steal_first(s->user_journals);
c2373f848dddcc1827cf715c5ef778dc8d475761Lennart Poettering r = journal_file_open(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = journal_file_rotate(&s->runtime_journal);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to rotate %s: %s", s->runtime_journal->path, strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = journal_file_rotate(&s->system_journal);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to rotate %s: %s", s->system_journal->path, strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to rotate %s: %s", f->path, strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to get machine ID: %s", strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (asprintf(&p, "/var/log/journal/%s", sd_id128_to_string(machine, ids)) < 0) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = journal_directory_vacuum(p, s->max_use, s->metrics.keep_free);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0 && r != -ENOENT)
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to vacuum %s: %s", p, strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (asprintf(&p, "/run/log/journal/%s", ids) < 0) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = journal_directory_vacuum(p, s->max_use, s->metrics.keep_free);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0 && r != -ENOENT)
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to vacuum %s: %s", p, strerror(-r));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic void dispatch_message(Server *s, struct iovec *iovec, unsigned n, unsigned m, struct ucred *ucred, struct timeval *tv) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *pid = NULL, *uid = NULL, *gid = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *comm = NULL, *cmdline = NULL, *hostname = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *audit_session = NULL, *audit_loginuid = NULL,
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&pid, "_PID=%lu", (unsigned long) ucred->pid) >= 0)
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&uid, "_UID=%lu", (unsigned long) ucred->uid) >= 0)
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&gid, "_GID=%lu", (unsigned long) ucred->gid) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = audit_session_from_pid(ucred->pid, &session);
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&audit_session, "_AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], audit_session);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = audit_loginuid_from_pid(ucred->pid, &loginuid);
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&audit_loginuid, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], audit_loginuid);
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, ucred->pid, &path);
de190aef08bb267b645205a747762df573b36834Lennart Poettering cgroup = strappend("_SYSTEMD_CGROUP=", path);
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (unsigned long long) timeval_load(tv)) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], source_time);
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering /* Note that strictly speaking storing the boot id here is
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering * redundant since the entry includes this in-line
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering * anyway. However, we need this indexed, too. */
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&boot_id, "_BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&machine_id, "_MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], machine_id);
de190aef08bb267b645205a747762df573b36834Lennart Poettering f = find_journal(s, realuid == 0 ? 0 : loginuid);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Dropping message, as we can't find a place to store the data.");
c2373f848dddcc1827cf715c5ef778dc8d475761Lennart Poettering r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_error("Failed to write entry, ignoring: %s", strerror(-r));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering unsigned n = 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering parse_syslog_priority((char**) &buf, &priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_facility);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv);
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poetteringstatic bool valid_user_field(const char *p, size_t l) {
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering const char *a;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* We kinda enforce POSIX syntax recommendations for
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering environment variables here, but make a couple of additional
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* No empty field names */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Don't allow names longer than 64 chars */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Variables starting with an underscore are protected */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (p[0] == '_')
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Don't allow digits as first character */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Only allow A-Z0-9 and '_' */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering for (a = p; a < p + l; a++)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic void process_native_message(Server *s, const void *buffer, size_t buffer_size, struct ucred *ucred, struct timeval *tv) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering unsigned n = 0, m = 0, j;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering const char *p;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering const char *e, *q;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Trailing noise, let's ignore it, and flush what we collected */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Received message with trailing noise, ignoring.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Entry separator */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering dispatch_message(s, iovec, n, m, ucred, tv);
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Ignore control commands for now, and
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering * comments too. */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* A property follows */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (n+13 >= m) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering c = realloc(iovec, u * sizeof(struct iovec));
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering /* If the field name starts with an
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering * underscore, skip the variable,
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering * since that indidates a trusted
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (remaining < e - p + 1 + sizeof(uint64_t) + 1) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Failed to parse message, ignoring.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Failed to parse message, ignoring.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering k[e - p] = '=';
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering dispatch_message(s, iovec, n, m, ucred, tv);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (j = 0; j < n; j++)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_log(StdoutStream *s, const char *p, size_t l) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering char *message = NULL, *syslog_priority = NULL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering unsigned n = 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering p[0] == '<' &&
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority) >= 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering tag_len = s->tag ? strlen(s->tag) + 2: 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering console = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering IOVEC_SET_STRING(iovec[n++], (char*) "\n");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_line(StdoutStream *s, const char *p, size_t l) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering while (l > 0 && strchr(WHITESPACE, *p)) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering while (l > 0 && strchr(WHITESPACE, *(p+l-1)))
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Failed to parse log priority line.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->state = STDOUT_STREAM_PRIORITY_PREFIX;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Failed to parse priority prefix line.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Failed to parse tee to console line.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert_not_reached("Unknown stream state");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_scan(StdoutStream *s, bool force_flush) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_process(StdoutStream *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Failed to read from stream: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic void stdout_stream_free(StdoutStream *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->fd >= 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to accept stdout connection: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Too many stdout streams, refusing connection.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to determine peer credentials: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to shutdown writing side of socket: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add stream to event loop: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int process_event(Server *s, struct epoll_event *ev) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering n = read(s->signal_fd, &sfsi, sizeof(sfsi));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (n != sizeof(sfsi)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (ev->data.fd == s->native_fd ||
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (ioctl(ev->data.fd, SIOCINQ, &v) < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering else if (cmsg->cmsg_level == SOL_SOCKET &&
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering process_syslog_message(s, strstrip(s->buffer), ucred, tv);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering process_native_message(s, s->buffer, n, ucred, tv);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (ev->data.fd == s->stdout_fd) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering /* If it is none of the well-known fds, it must be an
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * stdout stream fd. Note that this is a bit ugly here
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * (since we rely that none of the well-known fds
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * could be interpreted as pointer), but nonetheless
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * safe, since the well-known fds would never get an
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * fd > 4096, i.e. beyond the first memory page */
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poetteringstatic int system_journal_open(Server *s) {
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering /* First try to create the machine path, but not the prefix */
250d54b5bee6a46fe1c1626211e3a7e238eda628Lennart Poettering fn = strappend("/var/log/journal/", sd_id128_to_string(machine, ids));
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering /* The create the system journal file */
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering fn = join("/var/log/journal/", ids, "/system.journal", NULL);
0ac38b707212e9aa40e25d65ffbae648cc9116f5Lennart Poettering r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering s->system_journal->compress = s->compress;
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering if (r < 0 && r != -ENOENT) {
250d54b5bee6a46fe1c1626211e3a7e238eda628Lennart Poettering log_error("Failed to open system journal: %s", strerror(-r));
250d54b5bee6a46fe1c1626211e3a7e238eda628Lennart Poettering /* /var didn't work, so try /run, but this time we
250d54b5bee6a46fe1c1626211e3a7e238eda628Lennart Poettering * create the prefix too */
250d54b5bee6a46fe1c1626211e3a7e238eda628Lennart Poettering fn = join("/run/log/journal/", ids, "/system.journal", NULL);
0ac38b707212e9aa40e25d65ffbae648cc9116f5Lennart Poettering r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
250d54b5bee6a46fe1c1626211e3a7e238eda628Lennart Poettering log_error("Failed to open runtime journal: %s", strerror(-r));
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering s->runtime_journal->metrics = s->metrics;
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering s->runtime_journal->compress = s->compress;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int open_syslog_socket(Server *s) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add syslog server fd to epoll object: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering strncpy(sa.un.sun_path, "/run/systemd/journal", sizeof(sa.un.sun_path));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->native_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add native server fd to epoll object: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int open_stdout_socket(Server *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering strncpy(sa.un.sun_path, "/run/systemd/stdout", sizeof(sa.un.sun_path));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (listen(s->stdout_fd, SOMAXCONN) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add stdout server fd to epoll object: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering sigset_add_many(&mask, SIGINT, SIGTERM, -1);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->syslog_fd = s->native_fd = s->stdout_fd = s->signal_fd = s->epoll_fd = -1;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering s->metrics.keep_free = DEFAULT_KEEP_FREE;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Failed to create epoll object: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/native", 0) > 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Too many native sockets passed.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/stdout", 0) > 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Too many stdout sockets passed.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Too many /dev/log sockets passed.");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while ((f = hashmap_steal_first(s->user_journals)))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* if (getppid() != 1) { */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* log_error("This program should be invoked by init only."); */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* return EXIT_FAILURE; */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("This program does not take arguments.");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering "STATUS=Processing requests...");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = epoll_wait(server.epoll_fd, &event, 1, -1);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } else if (r == 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering else if (r == 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering "STATUS=Shutting down...");