journald-syslog.c revision 6baa7db00812437bbc87e73faa1a11b6cf631958
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering This file is part of systemd.
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering Copyright 2011 Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering systemd is free software; you can redistribute it and/or modify it
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering under the terms of the GNU Lesser General Public License as published by
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering (at your option) any later version.
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering systemd is distributed in the hope that it will be useful, but
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering Lesser General Public License for more details.
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering You should have received a copy of the GNU Lesser General Public License
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering/* Warn once every 30s if we missed syslog message */
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering#define WARN_FORWARD_SYSLOG_MISSED_USEC (30 * USEC_PER_SEC)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringstatic void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek .un.sun_path = "/run/systemd/journal/syslog",
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek .msg_iov = (struct iovec *) iovec,
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek .msg_namelen = offsetof(union sockaddr_union, un.sun_path)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* Forward the syslog message we received via /dev/log to
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * /run/systemd/syslog. Unfortunately we currently can't set
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * the SO_TIMESTAMP auxiliary data, and hence we don't. */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* The socket is full? I guess the syslog implementation is
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * too slow, and we shouldn't wait for that... */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* Hmm, presumably the sender process vanished
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * by now, so let's fix it as good as we
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * can, and retry */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering log_debug("Failed to forward syslog message: %m");
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringstatic void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (LOG_PRI(priority) > s->max_level_syslog)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering forward_syslog_iovec(s, &iovec, 1, ucred, tv);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringvoid server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering char header_priority[6], header_time[64], header_pid[16];
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (LOG_PRI(priority) > s->max_level_syslog)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* First: priority field */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_priority);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* Second: timestamp */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_time);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* Third: identifier and PID */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering get_process_comm(ucred->pid, &ident_buf);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", ucred->pid);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], identifier);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], identifier);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* Fourth: message */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering forward_syslog_iovec(s, iovec, n, ucred, tv);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering return (priority & LOG_PRIMASK) | LOG_USER;
e88baee88fad8bc59d33b55a7a2d640ef9e16cd6Zbigniew Jędrzejewski-Szmeksize_t syslog_parse_identifier(const char **buf, char **identifier, char **pid) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering const char *p;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (p[k] == '[') {
e3bfb7be07d9b1f4ebb12eb22c4c8bcd2a988d51Zbigniew Jędrzejewski-Szmekvoid syslog_parse_priority(const char **p, int *priority, bool with_facility) {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering int a = 0, b = 0, c = 0;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if ((*p)[0] != '<')
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek if (a < 0 || b < 0 || c < 0 ||
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek (!with_facility && (a || b || c > 7)))
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek *priority = (*priority & LOG_FACMASK) | c;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poetteringstatic void syslog_skip_date(char **buf) {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (*p != ' ')
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (*p == ' ')
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering /* fall through */
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (*p != ':')
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 6];
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering unsigned n = 0;
e3bfb7be07d9b1f4ebb12eb22c4c8bcd2a988d51Zbigniew Jędrzejewski-Szmek syslog_parse_priority(&buf, &priority, true);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering forward_syslog_raw(s, priority, orig, ucred, tv);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering syslog_parse_identifier(&buf, &identifier, &pid);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering server_forward_kmsg(s, priority, identifier, buf, ucred);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering server_forward_console(s, priority, identifier, buf, ucred);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen server_forward_wall(s, priority, identifier, buf, ucred);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_facility);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_identifier);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering syslog_pid = strappend("SYSLOG_PID=", pid);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_pid);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, NULL, priority, 0);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringint server_open_syslog_socket(Server *s) {
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poettering .un.sun_path = "/run/systemd/journal/dev-log",
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
6b9732b2bf0499c5e4ea8a9d4f6051d98033f680Zbigniew Jędrzejewski-Szmek log_error("bind(%s) failed: %m", sa.un.sun_path);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering r = sd_event_add_io(s->event, &s->syslog_event_source, s->syslog_fd, EPOLLIN, process_datagram, s);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_error("Failed to add syslog server fd to event loop: %s", strerror(-r));
178cc7700c23ac088cd7190d7854282075028d91Lennart Poetteringvoid server_maybe_warn_forward_syslog_missed(Server *s) {
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering if (s->last_warn_forward_syslog_missed + WARN_FORWARD_SYSLOG_MISSED_USEC > n)
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering server_driver_message(s, SD_MESSAGE_FORWARD_SYSLOG_MISSED, "Forwarding to syslog missed %u messages.", s->n_forward_syslog_missed);