summaryrefslogtreecommitdiff
path: root/src/journal/journald.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-08-22 03:21:03 +0200
committerLennart Poettering <lennart@poettering.net>2012-08-22 03:21:03 +0200
commit35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9 (patch)
treef30acf4ac399dfe2e4c0455163c60b38b4476e75 /src/journal/journald.c
parentef63833d532dd86bdba63211e6a1363cbb3ef61d (diff)
journald: splitt of syslog protocol support into its own file
Diffstat (limited to 'src/journal/journald.c')
-rw-r--r--src/journal/journald.c341
1 files changed, 11 insertions, 330 deletions
diff --git a/src/journal/journald.c b/src/journal/journald.c
index fbdd688c37..4b0ff53125 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -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;