journald-stream.c revision e14ddf1cac12f91685cbd5dac6c5127f8cf87863
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers This file is part of systemd.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers Copyright 2011 Lennart Poettering
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers systemd is free software; you can redistribute it and/or modify it
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers under the terms of the GNU Lesser General Public License as published by
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers the Free Software Foundation; either version 2.1 of the License, or
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers (at your option) any later version.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers systemd is distributed in the hope that it will be useful, but
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers WITHOUT ANY WARRANTY; without even the implied warranty of
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers Lesser General Public License for more details.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers You should have received a copy of the GNU Lesser General Public License
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers along with systemd; If not, see <http://www.gnu.org/licenses/>.
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic int stdout_stream_log(StdoutStream *s, const char *p) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers unsigned n = 0;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (s->forward_to_syslog || s->server->forward_to_syslog)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (s->forward_to_kmsg || s->server->forward_to_kmsg)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers server_forward_kmsg(s->server, priority, s->identifier, p, &s->ucred);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (s->forward_to_console || s->server->forward_to_console)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers server_forward_console(s->server, priority, s->identifier, p, &s->ucred);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers IOVEC_SET_STRING(iovec[n++], syslog_identifier);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers label_len = strlen((char*) s->security_context);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority);
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic int stdout_stream_line(StdoutStream *s, char *p) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers switch (s->state) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (r < 0 || s->priority < 0 || s->priority >= 999) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_warning("Failed to parse log priority line.");
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (r < 0) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_warning("Failed to parse level prefix line.");
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (r < 0) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_warning("Failed to parse forward to syslog line.");
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (r < 0) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_warning("Failed to parse copy to kmsg line.");
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (r < 0) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_warning("Failed to parse copy to console line.");
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic int stdout_stream_scan(StdoutStream *s, bool force_flush) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (p > s->buffer) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (l < 0) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (l == 0) {
if (s->fd >= 0) {
if (s->server)
#ifdef HAVE_SELINUX
if (s->security_context)
free(s);
int fd, r;
assert(s);
if (fd < 0) {
return -errno;
if (!stream) {
return log_oom();
r = -errno;
goto fail;
#ifdef HAVE_SELINUX
r = -errno;
goto fail;
r = -errno;
goto fail;
s->n_stdout_streams ++;
fail:
assert(s);
if (s->stdout_fd < 0) {
if (s->stdout_fd < 0) {
return -errno;
r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
return -errno;
return -errno;
return -errno;