mirror of
https://github.com/systemd/systemd.git
synced 2024-11-27 12:13:33 +08:00
journald: splitt of syslog protocol support into its own file
This commit is contained in:
parent
ef63833d53
commit
35e2e347d3
@ -2311,6 +2311,8 @@ systemd_journald_SOURCES = \
|
||||
src/journal/journald.h \
|
||||
src/journal/journald-kmsg.c \
|
||||
src/journal/journald-kmsg.h \
|
||||
src/journal/journald-syslog.c \
|
||||
src/journal/journald-syslog.h \
|
||||
src/journal/journal-rate-limit.c \
|
||||
src/journal/journal-rate-limit.h \
|
||||
src/journal/journal-internal.h
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
#include "journald.h"
|
||||
#include "journald-kmsg.h"
|
||||
#include "journald-syslog.h"
|
||||
|
||||
void server_forward_kmsg(
|
||||
Server *s,
|
||||
|
358
src/journal/journald-syslog.c
Normal file
358
src/journal/journald-syslog.c
Normal file
@ -0,0 +1,358 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2011 Lennart Poettering
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include "socket-util.h"
|
||||
#include "journald.h"
|
||||
#include "journald-syslog.h"
|
||||
#include "journald-kmsg.h"
|
||||
|
||||
static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
|
||||
struct msghdr msghdr;
|
||||
struct cmsghdr *cmsg;
|
||||
union {
|
||||
struct cmsghdr cmsghdr;
|
||||
uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
|
||||
} control;
|
||||
union sockaddr_union sa;
|
||||
|
||||
assert(s);
|
||||
assert(iovec);
|
||||
assert(n_iovec > 0);
|
||||
|
||||
zero(msghdr);
|
||||
msghdr.msg_iov = (struct iovec*) iovec;
|
||||
msghdr.msg_iovlen = n_iovec;
|
||||
|
||||
zero(sa);
|
||||
sa.un.sun_family = AF_UNIX;
|
||||
strncpy(sa.un.sun_path, "/run/systemd/journal/syslog", sizeof(sa.un.sun_path));
|
||||
msghdr.msg_name = &sa;
|
||||
msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
|
||||
|
||||
if (ucred) {
|
||||
zero(control);
|
||||
msghdr.msg_control = &control;
|
||||
msghdr.msg_controllen = sizeof(control);
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&msghdr);
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_CREDENTIALS;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
|
||||
memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
|
||||
msghdr.msg_controllen = cmsg->cmsg_len;
|
||||
}
|
||||
|
||||
/* Forward the syslog message we received via /dev/log to
|
||||
* /run/systemd/syslog. Unfortunately we currently can't set
|
||||
* the SO_TIMESTAMP auxiliary data, and hence we don't. */
|
||||
|
||||
if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
|
||||
return;
|
||||
|
||||
/* The socket is full? I guess the syslog implementation is
|
||||
* too slow, and we shouldn't wait for that... */
|
||||
if (errno == EAGAIN)
|
||||
return;
|
||||
|
||||
if (ucred && errno == ESRCH) {
|
||||
struct ucred u;
|
||||
|
||||
/* Hmm, presumably the sender process vanished
|
||||
* by now, so let's fix it as good as we
|
||||
* can, and retry */
|
||||
|
||||
u = *ucred;
|
||||
u.pid = getpid();
|
||||
memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
|
||||
|
||||
if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
|
||||
return;
|
||||
|
||||
if (errno == EAGAIN)
|
||||
return;
|
||||
}
|
||||
|
||||
if (errno != ENOENT)
|
||||
log_debug("Failed to forward syslog message: %m");
|
||||
}
|
||||
|
||||
static void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
|
||||
struct iovec iovec;
|
||||
|
||||
assert(s);
|
||||
assert(buffer);
|
||||
|
||||
if (LOG_PRI(priority) > s->max_level_syslog)
|
||||
return;
|
||||
|
||||
IOVEC_SET_STRING(iovec, buffer);
|
||||
forward_syslog_iovec(s, &iovec, 1, ucred, tv);
|
||||
}
|
||||
|
||||
void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
|
||||
struct iovec iovec[5];
|
||||
char header_priority[6], header_time[64], header_pid[16];
|
||||
int n = 0;
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
char *ident_buf = NULL;
|
||||
|
||||
assert(s);
|
||||
assert(priority >= 0);
|
||||
assert(priority <= 999);
|
||||
assert(message);
|
||||
|
||||
if (LOG_PRI(priority) > s->max_level_syslog)
|
||||
return;
|
||||
|
||||
/* First: priority field */
|
||||
snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
|
||||
char_array_0(header_priority);
|
||||
IOVEC_SET_STRING(iovec[n++], header_priority);
|
||||
|
||||
/* Second: timestamp */
|
||||
t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
|
||||
tm = localtime(&t);
|
||||
if (!tm)
|
||||
return;
|
||||
if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
|
||||
return;
|
||||
IOVEC_SET_STRING(iovec[n++], header_time);
|
||||
|
||||
/* Third: identifier and PID */
|
||||
if (ucred) {
|
||||
if (!identifier) {
|
||||
get_process_comm(ucred->pid, &ident_buf);
|
||||
identifier = ident_buf;
|
||||
}
|
||||
|
||||
snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
|
||||
char_array_0(header_pid);
|
||||
|
||||
if (identifier)
|
||||
IOVEC_SET_STRING(iovec[n++], identifier);
|
||||
|
||||
IOVEC_SET_STRING(iovec[n++], header_pid);
|
||||
} else if (identifier) {
|
||||
IOVEC_SET_STRING(iovec[n++], identifier);
|
||||
IOVEC_SET_STRING(iovec[n++], ": ");
|
||||
}
|
||||
|
||||
/* Fourth: message */
|
||||
IOVEC_SET_STRING(iovec[n++], message);
|
||||
|
||||
forward_syslog_iovec(s, iovec, n, ucred, tv);
|
||||
|
||||
free(ident_buf);
|
||||
}
|
||||
|
||||
int syslog_fixup_facility(int priority) {
|
||||
|
||||
if ((priority & LOG_FACMASK) == 0)
|
||||
return (priority & LOG_PRIMASK) | LOG_USER;
|
||||
|
||||
return priority;
|
||||
}
|
||||
|
||||
void syslog_read_identifier(const char **buf, char **identifier, char **pid) {
|
||||
const char *p;
|
||||
char *t;
|
||||
size_t l, e;
|
||||
|
||||
assert(buf);
|
||||
assert(identifier);
|
||||
assert(pid);
|
||||
|
||||
p = *buf;
|
||||
|
||||
p += strspn(p, WHITESPACE);
|
||||
l = strcspn(p, WHITESPACE);
|
||||
|
||||
if (l <= 0 ||
|
||||
p[l-1] != ':')
|
||||
return;
|
||||
|
||||
e = l;
|
||||
l--;
|
||||
|
||||
if (p[l-1] == ']') {
|
||||
size_t k = l-1;
|
||||
|
||||
for (;;) {
|
||||
|
||||
if (p[k] == '[') {
|
||||
t = strndup(p+k+1, l-k-2);
|
||||
if (t)
|
||||
*pid = t;
|
||||
|
||||
l = k;
|
||||
break;
|
||||
}
|
||||
|
||||
if (k == 0)
|
||||
break;
|
||||
|
||||
k--;
|
||||
}
|
||||
}
|
||||
|
||||
t = strndup(p, l);
|
||||
if (t)
|
||||
*identifier = t;
|
||||
|
||||
*buf = p + e;
|
||||
*buf += strspn(*buf, WHITESPACE);
|
||||
}
|
||||
|
||||
void server_process_syslog_message(
|
||||
Server *s,
|
||||
const char *buf,
|
||||
struct ucred *ucred,
|
||||
struct timeval *tv,
|
||||
const char *label,
|
||||
size_t label_len) {
|
||||
|
||||
char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
|
||||
struct iovec iovec[N_IOVEC_META_FIELDS + 6];
|
||||
unsigned n = 0;
|
||||
int priority = LOG_USER | LOG_INFO;
|
||||
char *identifier = NULL, *pid = NULL;
|
||||
const char *orig;
|
||||
|
||||
assert(s);
|
||||
assert(buf);
|
||||
|
||||
orig = buf;
|
||||
syslog_parse_priority((char**) &buf, &priority);
|
||||
|
||||
if (s->forward_to_syslog)
|
||||
forward_syslog_raw(s, priority, orig, ucred, tv);
|
||||
|
||||
syslog_skip_date((char**) &buf);
|
||||
syslog_read_identifier(&buf, &identifier, &pid);
|
||||
|
||||
if (s->forward_to_kmsg)
|
||||
server_forward_kmsg(s, priority, identifier, buf, ucred);
|
||||
|
||||
if (s->forward_to_console)
|
||||
server_forward_console(s, priority, identifier, buf, ucred);
|
||||
|
||||
IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
|
||||
|
||||
if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_priority);
|
||||
|
||||
if (priority & LOG_FACMASK)
|
||||
if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_facility);
|
||||
|
||||
if (identifier) {
|
||||
syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
|
||||
if (syslog_identifier)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_identifier);
|
||||
}
|
||||
|
||||
if (pid) {
|
||||
syslog_pid = strappend("SYSLOG_PID=", pid);
|
||||
if (syslog_pid)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_pid);
|
||||
}
|
||||
|
||||
message = strappend("MESSAGE=", buf);
|
||||
if (message)
|
||||
IOVEC_SET_STRING(iovec[n++], message);
|
||||
|
||||
server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, NULL, priority);
|
||||
|
||||
free(message);
|
||||
free(identifier);
|
||||
free(pid);
|
||||
free(syslog_priority);
|
||||
free(syslog_facility);
|
||||
free(syslog_identifier);
|
||||
free(syslog_pid);
|
||||
}
|
||||
|
||||
int server_open_syslog_socket(Server *s) {
|
||||
union sockaddr_union sa;
|
||||
int one, r;
|
||||
struct epoll_event ev;
|
||||
|
||||
assert(s);
|
||||
|
||||
if (s->syslog_fd < 0) {
|
||||
|
||||
s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
||||
if (s->syslog_fd < 0) {
|
||||
log_error("socket() failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
zero(sa);
|
||||
sa.un.sun_family = AF_UNIX;
|
||||
strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
|
||||
|
||||
unlink(sa.un.sun_path);
|
||||
|
||||
r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
|
||||
if (r < 0) {
|
||||
log_error("bind() failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
chmod(sa.un.sun_path, 0666);
|
||||
} else
|
||||
fd_nonblock(s->syslog_fd, 1);
|
||||
|
||||
one = 1;
|
||||
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
|
||||
if (r < 0) {
|
||||
log_error("SO_PASSCRED failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SELINUX
|
||||
one = 1;
|
||||
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
|
||||
if (r < 0)
|
||||
log_warning("SO_PASSSEC failed: %m");
|
||||
#endif
|
||||
|
||||
one = 1;
|
||||
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
|
||||
if (r < 0) {
|
||||
log_error("SO_TIMESTAMP failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
zero(ev);
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = s->syslog_fd;
|
||||
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
|
||||
log_error("Failed to add syslog server fd to epoll object: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
32
src/journal/journald-syslog.h
Normal file
32
src/journal/journald-syslog.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2011 Lennart Poettering
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include "journald.h"
|
||||
|
||||
void syslog_read_identifier(const char **buf, char **identifier, char **pid);
|
||||
int syslog_fixup_facility(int priority);
|
||||
|
||||
void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv);
|
||||
|
||||
void server_process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv, const char *label, size_t label_len);
|
||||
int server_open_syslog_socket(Server *s);
|
@ -46,15 +46,16 @@
|
||||
#include "socket-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "list.h"
|
||||
#include "virt.h"
|
||||
#include "missing.h"
|
||||
#include "conf-parser.h"
|
||||
#include "journal-rate-limit.h"
|
||||
#include "journal-internal.h"
|
||||
#include "journal-vacuum.h"
|
||||
#include "journal-authenticate.h"
|
||||
#include "conf-parser.h"
|
||||
#include "journald.h"
|
||||
#include "journald-kmsg.h"
|
||||
#include "virt.h"
|
||||
#include "missing.h"
|
||||
#include "journald-syslog.h"
|
||||
|
||||
#ifdef HAVE_ACL
|
||||
#include <sys/acl.h>
|
||||
@ -807,147 +808,7 @@ finish:
|
||||
dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id);
|
||||
}
|
||||
|
||||
static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
|
||||
struct msghdr msghdr;
|
||||
struct cmsghdr *cmsg;
|
||||
union {
|
||||
struct cmsghdr cmsghdr;
|
||||
uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
|
||||
} control;
|
||||
union sockaddr_union sa;
|
||||
|
||||
assert(s);
|
||||
assert(iovec);
|
||||
assert(n_iovec > 0);
|
||||
|
||||
zero(msghdr);
|
||||
msghdr.msg_iov = (struct iovec*) iovec;
|
||||
msghdr.msg_iovlen = n_iovec;
|
||||
|
||||
zero(sa);
|
||||
sa.un.sun_family = AF_UNIX;
|
||||
strncpy(sa.un.sun_path, "/run/systemd/journal/syslog", sizeof(sa.un.sun_path));
|
||||
msghdr.msg_name = &sa;
|
||||
msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
|
||||
|
||||
if (ucred) {
|
||||
zero(control);
|
||||
msghdr.msg_control = &control;
|
||||
msghdr.msg_controllen = sizeof(control);
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&msghdr);
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_CREDENTIALS;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
|
||||
memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
|
||||
msghdr.msg_controllen = cmsg->cmsg_len;
|
||||
}
|
||||
|
||||
/* Forward the syslog message we received via /dev/log to
|
||||
* /run/systemd/syslog. Unfortunately we currently can't set
|
||||
* the SO_TIMESTAMP auxiliary data, and hence we don't. */
|
||||
|
||||
if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
|
||||
return;
|
||||
|
||||
/* The socket is full? I guess the syslog implementation is
|
||||
* too slow, and we shouldn't wait for that... */
|
||||
if (errno == EAGAIN)
|
||||
return;
|
||||
|
||||
if (ucred && errno == ESRCH) {
|
||||
struct ucred u;
|
||||
|
||||
/* Hmm, presumably the sender process vanished
|
||||
* by now, so let's fix it as good as we
|
||||
* can, and retry */
|
||||
|
||||
u = *ucred;
|
||||
u.pid = getpid();
|
||||
memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
|
||||
|
||||
if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
|
||||
return;
|
||||
|
||||
if (errno == EAGAIN)
|
||||
return;
|
||||
}
|
||||
|
||||
if (errno != ENOENT)
|
||||
log_debug("Failed to forward syslog message: %m");
|
||||
}
|
||||
|
||||
static void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
|
||||
struct iovec iovec;
|
||||
|
||||
assert(s);
|
||||
assert(buffer);
|
||||
|
||||
if (LOG_PRI(priority) > s->max_level_syslog)
|
||||
return;
|
||||
|
||||
IOVEC_SET_STRING(iovec, buffer);
|
||||
forward_syslog_iovec(s, &iovec, 1, ucred, tv);
|
||||
}
|
||||
|
||||
static void forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
|
||||
struct iovec iovec[5];
|
||||
char header_priority[6], header_time[64], header_pid[16];
|
||||
int n = 0;
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
char *ident_buf = NULL;
|
||||
|
||||
assert(s);
|
||||
assert(priority >= 0);
|
||||
assert(priority <= 999);
|
||||
assert(message);
|
||||
|
||||
if (LOG_PRI(priority) > s->max_level_syslog)
|
||||
return;
|
||||
|
||||
/* First: priority field */
|
||||
snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
|
||||
char_array_0(header_priority);
|
||||
IOVEC_SET_STRING(iovec[n++], header_priority);
|
||||
|
||||
/* Second: timestamp */
|
||||
t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
|
||||
tm = localtime(&t);
|
||||
if (!tm)
|
||||
return;
|
||||
if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
|
||||
return;
|
||||
IOVEC_SET_STRING(iovec[n++], header_time);
|
||||
|
||||
/* Third: identifier and PID */
|
||||
if (ucred) {
|
||||
if (!identifier) {
|
||||
get_process_comm(ucred->pid, &ident_buf);
|
||||
identifier = ident_buf;
|
||||
}
|
||||
|
||||
snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
|
||||
char_array_0(header_pid);
|
||||
|
||||
if (identifier)
|
||||
IOVEC_SET_STRING(iovec[n++], identifier);
|
||||
|
||||
IOVEC_SET_STRING(iovec[n++], header_pid);
|
||||
} else if (identifier) {
|
||||
IOVEC_SET_STRING(iovec[n++], identifier);
|
||||
IOVEC_SET_STRING(iovec[n++], ": ");
|
||||
}
|
||||
|
||||
/* Fourth: message */
|
||||
IOVEC_SET_STRING(iovec[n++], message);
|
||||
|
||||
forward_syslog_iovec(s, iovec, n, ucred, tv);
|
||||
|
||||
free(ident_buf);
|
||||
}
|
||||
|
||||
static void forward_console(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
|
||||
void server_forward_console(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
|
||||
struct iovec iovec[4];
|
||||
char header_pid[16];
|
||||
int n = 0, fd;
|
||||
@ -1000,125 +861,7 @@ finish:
|
||||
free(ident_buf);
|
||||
}
|
||||
|
||||
int syslog_fixup_facility(int priority) {
|
||||
|
||||
if ((priority & LOG_FACMASK) == 0)
|
||||
return (priority & LOG_PRIMASK) | LOG_USER;
|
||||
|
||||
return priority;
|
||||
}
|
||||
|
||||
void syslog_read_identifier(const char **buf, char **identifier, char **pid) {
|
||||
const char *p;
|
||||
char *t;
|
||||
size_t l, e;
|
||||
|
||||
assert(buf);
|
||||
assert(identifier);
|
||||
assert(pid);
|
||||
|
||||
p = *buf;
|
||||
|
||||
p += strspn(p, WHITESPACE);
|
||||
l = strcspn(p, WHITESPACE);
|
||||
|
||||
if (l <= 0 ||
|
||||
p[l-1] != ':')
|
||||
return;
|
||||
|
||||
e = l;
|
||||
l--;
|
||||
|
||||
if (p[l-1] == ']') {
|
||||
size_t k = l-1;
|
||||
|
||||
for (;;) {
|
||||
|
||||
if (p[k] == '[') {
|
||||
t = strndup(p+k+1, l-k-2);
|
||||
if (t)
|
||||
*pid = t;
|
||||
|
||||
l = k;
|
||||
break;
|
||||
}
|
||||
|
||||
if (k == 0)
|
||||
break;
|
||||
|
||||
k--;
|
||||
}
|
||||
}
|
||||
|
||||
t = strndup(p, l);
|
||||
if (t)
|
||||
*identifier = t;
|
||||
|
||||
*buf = p + e;
|
||||
*buf += strspn(*buf, WHITESPACE);
|
||||
}
|
||||
|
||||
static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv, const char *label, size_t label_len) {
|
||||
char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
|
||||
struct iovec iovec[N_IOVEC_META_FIELDS + 6];
|
||||
unsigned n = 0;
|
||||
int priority = LOG_USER | LOG_INFO;
|
||||
char *identifier = NULL, *pid = NULL;
|
||||
const char *orig;
|
||||
|
||||
assert(s);
|
||||
assert(buf);
|
||||
|
||||
orig = buf;
|
||||
syslog_parse_priority((char**) &buf, &priority);
|
||||
|
||||
if (s->forward_to_syslog)
|
||||
forward_syslog_raw(s, priority, orig, ucred, tv);
|
||||
|
||||
syslog_skip_date((char**) &buf);
|
||||
syslog_read_identifier(&buf, &identifier, &pid);
|
||||
|
||||
if (s->forward_to_kmsg)
|
||||
server_forward_kmsg(s, priority, identifier, buf, ucred);
|
||||
|
||||
if (s->forward_to_console)
|
||||
forward_console(s, priority, identifier, buf, ucred);
|
||||
|
||||
IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
|
||||
|
||||
if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_priority);
|
||||
|
||||
if (priority & LOG_FACMASK)
|
||||
if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_facility);
|
||||
|
||||
if (identifier) {
|
||||
syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
|
||||
if (syslog_identifier)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_identifier);
|
||||
}
|
||||
|
||||
if (pid) {
|
||||
syslog_pid = strappend("SYSLOG_PID=", pid);
|
||||
if (syslog_pid)
|
||||
IOVEC_SET_STRING(iovec[n++], syslog_pid);
|
||||
}
|
||||
|
||||
message = strappend("MESSAGE=", buf);
|
||||
if (message)
|
||||
IOVEC_SET_STRING(iovec[n++], message);
|
||||
|
||||
server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, NULL, priority);
|
||||
|
||||
free(message);
|
||||
free(identifier);
|
||||
free(pid);
|
||||
free(syslog_priority);
|
||||
free(syslog_facility);
|
||||
free(syslog_identifier);
|
||||
free(syslog_pid);
|
||||
}
|
||||
|
||||
static bool valid_user_field(const char *p, size_t l) {
|
||||
const char *a;
|
||||
@ -1329,13 +1072,13 @@ static void process_native_message(
|
||||
|
||||
if (message) {
|
||||
if (s->forward_to_syslog)
|
||||
forward_syslog(s, priority, identifier, message, ucred, tv);
|
||||
server_forward_syslog(s, priority, identifier, message, ucred, tv);
|
||||
|
||||
if (s->forward_to_kmsg)
|
||||
server_forward_kmsg(s, priority, identifier, message, ucred);
|
||||
|
||||
if (s->forward_to_console)
|
||||
forward_console(s, priority, identifier, message, ucred);
|
||||
server_forward_console(s, priority, identifier, message, ucred);
|
||||
}
|
||||
|
||||
server_dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority);
|
||||
@ -1427,13 +1170,13 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
|
||||
syslog_parse_priority((char**) &p, &priority);
|
||||
|
||||
if (s->forward_to_syslog || s->server->forward_to_syslog)
|
||||
forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
|
||||
server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
|
||||
|
||||
if (s->forward_to_kmsg || s->server->forward_to_kmsg)
|
||||
server_forward_kmsg(s->server, priority, s->identifier, p, &s->ucred);
|
||||
|
||||
if (s->forward_to_console || s->server->forward_to_console)
|
||||
forward_console(s->server, priority, s->identifier, p, &s->ucred);
|
||||
server_forward_console(s->server, priority, s->identifier, p, &s->ucred);
|
||||
|
||||
IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
|
||||
|
||||
@ -2081,7 +1824,7 @@ static int process_event(Server *s, struct epoll_event *ev) {
|
||||
else
|
||||
s->buffer[n] = 0;
|
||||
|
||||
process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
|
||||
server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
|
||||
} else if (n_fds > 0)
|
||||
log_warning("Got file descriptors via syslog socket. Ignoring.");
|
||||
|
||||
@ -2136,68 +1879,6 @@ static int process_event(Server *s, struct epoll_event *ev) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int open_syslog_socket(Server *s) {
|
||||
union sockaddr_union sa;
|
||||
int one, r;
|
||||
struct epoll_event ev;
|
||||
|
||||
assert(s);
|
||||
|
||||
if (s->syslog_fd < 0) {
|
||||
|
||||
s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
||||
if (s->syslog_fd < 0) {
|
||||
log_error("socket() failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
zero(sa);
|
||||
sa.un.sun_family = AF_UNIX;
|
||||
strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
|
||||
|
||||
unlink(sa.un.sun_path);
|
||||
|
||||
r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
|
||||
if (r < 0) {
|
||||
log_error("bind() failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
chmod(sa.un.sun_path, 0666);
|
||||
} else
|
||||
fd_nonblock(s->syslog_fd, 1);
|
||||
|
||||
one = 1;
|
||||
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
|
||||
if (r < 0) {
|
||||
log_error("SO_PASSCRED failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SELINUX
|
||||
one = 1;
|
||||
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
|
||||
if (r < 0)
|
||||
log_warning("SO_PASSSEC failed: %m");
|
||||
#endif
|
||||
|
||||
one = 1;
|
||||
r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
|
||||
if (r < 0) {
|
||||
log_error("SO_TIMESTAMP failed: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
zero(ev);
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = s->syslog_fd;
|
||||
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
|
||||
log_error("Failed to add syslog server fd to epoll object: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int open_native_socket(Server*s) {
|
||||
union sockaddr_union sa;
|
||||
@ -2500,7 +2181,7 @@ static int server_init(Server *s) {
|
||||
}
|
||||
}
|
||||
|
||||
r = open_syslog_socket(s);
|
||||
r = server_open_syslog_socket(s);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
@ -119,5 +119,4 @@ int config_parse_storage(const char *filename, unsigned line, const char *sectio
|
||||
const char *storage_to_string(Storage s);
|
||||
Storage storage_from_string(const char *s);
|
||||
|
||||
int syslog_fixup_facility(int priority);
|
||||
void syslog_read_identifier(const char **buf, char **identifier, char **pid);
|
||||
void server_forward_console(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred);
|
||||
|
Loading…
Reference in New Issue
Block a user