journald.c revision 81527be142678057215665be66e4b3c8306a7ab3
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/>.
de97b26ac5e29063632312ec1a20eb6318ca924cLennart Poettering#define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering#define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering#define RECHECK_VAR_AVAILABLE_USEC (30*USEC_PER_SEC)
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering#define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering LIST_FIELDS(StdoutStream, stdout_stream);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringstatic int server_flush_to_var(Server *s);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringstatic uint64_t available_space(Server *s) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering const char *f;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering uint64_t sum = 0, avail = 0, ss_avail = 0;
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering p = strappend(f, sd_id128_to_string(machine, ids));
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!dirent_is_file_with_suffix(de, ".journal"))
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering sum += (uint64_t) st.st_blocks * (uint64_t) st.st_blksize;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering avail = sum >= m->max_use ? 0 : m->max_use - sum;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering ss_avail = ss_avail < m->keep_free ? 0 : ss_avail - m->keep_free;
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering s->cached_available_space_timestamp = ts;
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) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* We split up user logs only on /var, not on /run. If the
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * runtime file is open, we write to it exclusively, in order
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * to guarantee proper order as soon as we flush /run to
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * /var and close the runtime file. */
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));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (asprintf(&p, "/var/log/journal/%s", ids) < 0) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (r < 0 && r != -ENOENT)
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering log_error("Failed to vacuum %s: %s", p, strerror(-r));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (asprintf(&p, "/run/log/journal/%s", ids) < 0) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (r < 0 && r != -ENOENT)
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering log_error("Failed to vacuum %s: %s", p, strerror(-r));
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringstatic char *shortened_cgroup_path(pid_t pid) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &process_path);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &init_path);
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering if (startswith(process_path, init_path)) {
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering p = strdup(process_path + strlen(init_path));
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringstatic void dispatch_message_real(Server *s,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct iovec *iovec, unsigned n, unsigned m,
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *pid = NULL, *uid = NULL, *gid = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *comm = NULL, *cmdline = NULL, *hostname = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *audit_session = NULL, *audit_loginuid = NULL,
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering *exe = NULL, *cgroup = NULL, *session = NULL,
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);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering r = audit_session_from_pid(ucred->pid, &audit);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (asprintf(&audit_session, "_AUDIT_SESSION=%lu", (unsigned long) audit) >= 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);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering cgroup = strappend("_SYSTEMD_CGROUP=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (sd_pid_get_session(ucred->pid, &t) >= 0) {
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering session = strappend("_SYSTEMD_SESSION=", t);
94fb446e55babb713fb24850455627acf30d999bLennart Poettering if (sd_pid_get_unit(ucred->pid, &t) >= 0) {
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (sd_pid_get_owner_uid(ucred->uid, &owner) >= 0)
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (asprintf(&owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner) >= 0)
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));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 3];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], "PRIORITY=5");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(mid, sizeof(mid), "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(message_id));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct iovec *iovec, unsigned n, unsigned m,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering path = shortened_cgroup_path(ucred->pid);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering /* example: /user/lennart/3/foobar
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * So let's cut of everything past the third /, since that is
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * wher user directories start */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering rl = journal_rate_limit_test(s->rate_limit, path, priority & LOG_PRIMASK, available_space(s));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Write a suppression message if we suppressed something */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering driver_message(s, SD_MESSAGE_JOURNAL_DROPPED, "Suppressed %u messages from %s", rl - 1, path);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message_real(s, iovec, n, m, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Forward the syslog message we received via /dev/log to
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * /run/systemd/syslog. Unfortunately we currently can't set
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * the SO_TIMESTAMP auxiliary data, and hence we don't. */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Hmm, presumably the sender process vanished
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * by now, so let's fix it as good as we
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * can, and retry */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to forward syslog message: %m");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_syslog_raw(Server *s, const char *buffer, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog_iovec(s, &iovec, 1, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_syslog(Server *s, int priority, const char *tag, const char *message, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char header_priority[6], header_time[64], header_pid[16];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* First: priority field */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Second: timestamp */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_time);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Third: tag and PID */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Fourth: message */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog_iovec(s, iovec, n, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return (priority & LOG_PRIMASK) | LOG_USER;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_kmsg(Server *s, int priority, const char *tag, const char *message, struct ucred *ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Never allow messages with kernel facility to be written to
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * kmsg, regardless where the data comes from. */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* First: priority field */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Second: tag and PID */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Fourth: message */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to open /dev/kmsg for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to write to /dev/kmsg for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_console(Server *s, const char *tag, const char *message, struct ucred *ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* First: tag and PID */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Third: message */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to open /dev/console for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to write to /dev/console for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void read_tag(const char **buf, char **tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering const char *p;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (p[k] == '[') {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_tag = NULL;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 4];
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering unsigned n = 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering parse_syslog_priority((char**) &buf, &priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_kmsg(s, priority, tag, buf, ucred);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_facility);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering syslog_tag = strappend("SYSLOG_TAG=", tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, priority);
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 */
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering dispatch_message(s, iovec, n, m, ucred, tv, priority);
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Ignore control commands for now, and
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering * comments too. */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* A property follows */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering u = MAX((n+N_IOVEC_META_FIELDS) * 2U, 4U);
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
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering /* We need to determine the priority
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * of this entry for the rate limiting
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (l == 10 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering priority = (priority & LOG_FACMASK) | (p[9] - '0');
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (l == 17 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering priority = (priority & LOG_PRIMASK) | ((p[16] - '0') << 3);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (l == 18 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering priority = (priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (l >= 12 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering } else if (l >= 8 &&
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;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog(s, priority, tag, message, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_kmsg(s, priority, tag, message, ucred);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering dispatch_message(s, iovec, n, m, ucred, tv, priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (j = 0; j < n; j++)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic int stdout_stream_log(StdoutStream *s, const char *p) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 4];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_tag = NULL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering unsigned n = 0;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering parse_syslog_priority((char**) &p, &priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_syslog || s->server->forward_to_syslog)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog(s->server, fixup_priority(priority), s->tag, p, &s->ucred, NULL);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_kmsg || s->server->forward_to_kmsg)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_kmsg(s->server, priority, s->tag, p, &s->ucred);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_console || s->server->forward_to_console)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_console(s->server, s->tag, p, &s->ucred);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_facility);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering syslog_tag = strappend("SYSLOG_TAG=", s->tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic int stdout_stream_line(StdoutStream *s, char *p) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (r < 0 || s->priority <= 0 || s->priority >= 999) {
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.");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_SYSLOG;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_warning("Failed to parse forward to syslog line.");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_KMSG;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_warning("Failed to parse copy to kmsg line.");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_CONSOLE;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_warning("Failed to parse copy to console line.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert_not_reached("Unknown stream state");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_scan(StdoutStream *s, bool force_flush) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (remaining >= sizeof(s->buffer) - 1) {
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);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringstatic int system_journal_open(Server *s) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* First try to create the machine path, but not the prefix */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fn = strappend("/var/log/journal/", ids);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* The create the system journal file */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fn = join("/var/log/journal/", ids, "/system.journal", NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering journal_default_metrics(&s->system_metrics, s->system_journal->fd);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering s->system_journal->metrics = s->system_metrics;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering s->system_journal->compress = s->compress;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering } else if (r < 0) {
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering log_warning("Failed to open system journal: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fn = join("/run/log/journal/", ids, "/system.journal", NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* Try to open the runtime journal, but only
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * if it already exists, so that we can flush
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * it into the system journal */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_open(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering log_warning("Failed to open runtime journal: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* OK, we really need the runtime journal, so create
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * it if necessary. */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Failed to open runtime journal: %s", strerror(-r));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering journal_default_metrics(&s->runtime_metrics, s->runtime_journal->fd);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering s->runtime_journal->metrics = s->runtime_metrics;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering s->runtime_journal->compress = s->compress;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringstatic int server_flush_to_var(Server *s) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering if (s->var_available_timestamp + RECHECK_VAR_AVAILABLE_USEC > ts)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Failed to get machine id: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Failed to read runtime journal: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Can't read entry: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering journal_file_post_change(s->system_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Can't write entry: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering journal_file_post_change(s->system_journal);
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 */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int open_syslog_socket(Server *s) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering strncpy(sa.un.sun_path, "/dev/log", 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));
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering /* Since we use the same socket for forwarding this to some
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering * other syslog implementation, make sure we don't hang
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering if (setsockopt(s->syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
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");
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, -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) {
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poetteringstatic int server_parse_config_file(Server *s) {
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering fn = "/etc/systemd/systemd-journald.conf";
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering log_warning("Failed to open configuration file %s: %m", fn);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering r = config_parse(fn, f, "Journal\0", config_item_perf_lookup, (void*) journald_gperf_lookup, false, s);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering log_warning("Failed to parse configuration file: %s", strerror(-r));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->syslog_fd = s->native_fd = s->stdout_fd = s->signal_fd = s->epoll_fd = -1;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
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.");
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
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());
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
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());
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering "STATUS=Shutting down...");