journald-stream.c revision d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering/***
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering This file is part of systemd.
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering Copyright 2011 Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering systemd is free software; you can redistribute it and/or modify it
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering under the terms of the GNU Lesser General Public License as published by
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering (at your option) any later version.
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering systemd is distributed in the hope that it will be useful, but
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering Lesser General Public License for more details.
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering You should have received a copy of the GNU Lesser General Public License
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering***/
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include <fcntl.h>
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include <unistd.h>
4871690d9e32608bbd9b18505b5326c2079c9690Allin Cottrell#include <stddef.h>
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include <sys/epoll.h>
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#ifdef HAVE_SELINUX
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include <selinux/selinux.h>
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#endif
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include "socket-util.h"
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering#include "selinux-util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "journald-server.h"
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include "journald-stream.h"
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include "journald-syslog.h"
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include "journald-kmsg.h"
3b7124a8db56ed57525b9ecfd19cfdc8c9facba0Lennart Poettering#include "journald-console.h"
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#define STDOUT_STREAMS_MAX 4096
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringtypedef enum StdoutStreamState {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_IDENTIFIER,
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_UNIT_ID,
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_PRIORITY,
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_LEVEL_PREFIX,
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_FORWARD_TO_SYSLOG,
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_FORWARD_TO_KMSG,
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_FORWARD_TO_CONSOLE,
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering STDOUT_STREAM_RUNNING
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering} StdoutStreamState;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringstruct StdoutStream {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering Server *server;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering StdoutStreamState state;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int fd;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering struct ucred ucred;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#ifdef HAVE_SELINUX
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering security_context_t security_context;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#endif
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering char *identifier;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering char *unit_id;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int priority;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering bool level_prefix:1;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering bool forward_to_syslog:1;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering bool forward_to_kmsg:1;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering bool forward_to_console:1;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering char buffer[LINE_MAX+1];
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering size_t length;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering LIST_FIELDS(StdoutStream, stdout_stream);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering};
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringstatic int stdout_stream_log(StdoutStream *s, const char *p) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 5];
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering unsigned n = 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int priority;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering char *label = NULL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering size_t label_len = 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (isempty(p))
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering priority = s->priority;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->level_prefix)
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek syslog_parse_priority((char**) &p, &priority, false);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->forward_to_syslog || s->server->forward_to_syslog)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->forward_to_kmsg || s->server->forward_to_kmsg)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering server_forward_kmsg(s->server, priority, s->identifier, p, &s->ucred);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->forward_to_console || s->server->forward_to_console)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering server_forward_console(s->server, priority, s->identifier, p, &s->ucred);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (priority & LOG_FACMASK)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_facility);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->identifier) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (syslog_identifier)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_identifier);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering message = strappend("MESSAGE=", p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (message)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering IOVEC_SET_STRING(iovec[n++], message);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#ifdef HAVE_SELINUX
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->security_context) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering label = (char*) s->security_context;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering label_len = strlen((char*) s->security_context);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#endif
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority, 0);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering free(message);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering free(syslog_priority);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering free(syslog_facility);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering free(syslog_identifier);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering}
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringstatic int stdout_stream_line(StdoutStream *s, char *p) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering p = strstrip(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering switch (s->state) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_IDENTIFIER:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (isempty(p))
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->identifier = NULL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering else {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->identifier = strdup(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (!s->identifier)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return log_oom();
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->state = STDOUT_STREAM_UNIT_ID;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_UNIT_ID:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->ucred.uid == 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (isempty(p))
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->unit_id = NULL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering else {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->unit_id = strdup(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (!s->unit_id)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return log_oom();
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->state = STDOUT_STREAM_PRIORITY;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_PRIORITY:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = safe_atoi(p, &s->priority);
41891700e02daf0cab9e86908c76ac6f411bbd57Lennart Poettering if (r < 0 || s->priority < 0 || s->priority > 999) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_warning("Failed to parse log priority line.");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -EINVAL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->state = STDOUT_STREAM_LEVEL_PREFIX;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_LEVEL_PREFIX:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = parse_boolean(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_warning("Failed to parse level prefix line.");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -EINVAL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->level_prefix = !!r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_SYSLOG;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_FORWARD_TO_SYSLOG:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = parse_boolean(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_warning("Failed to parse forward to syslog line.");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -EINVAL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->forward_to_syslog = !!r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_KMSG;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_FORWARD_TO_KMSG:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = parse_boolean(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_warning("Failed to parse copy to kmsg line.");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -EINVAL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->forward_to_kmsg = !!r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_CONSOLE;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_FORWARD_TO_CONSOLE:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = parse_boolean(p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_warning("Failed to parse copy to console line.");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -EINVAL;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->forward_to_console = !!r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->state = STDOUT_STREAM_RUNNING;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering case STDOUT_STREAM_RUNNING:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return stdout_stream_log(s, p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert_not_reached("Unknown stream state");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering}
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringstatic int stdout_stream_scan(StdoutStream *s, bool force_flush) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering char *p;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering size_t remaining;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering p = s->buffer;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering remaining = s->length;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering for (;;) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering char *end;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering size_t skip;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering end = memchr(p, '\n', remaining);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (end)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering skip = end - p + 1;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering else if (remaining >= sizeof(s->buffer) - 1) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering end = p + sizeof(s->buffer) - 1;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering skip = remaining;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering } else
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering break;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering *end = 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = stdout_stream_line(s, p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering remaining -= skip;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering p += skip;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (force_flush && remaining > 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering p[remaining] = 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = stdout_stream_line(s, p);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering p += remaining;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering remaining = 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (p > s->buffer) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering memmove(s->buffer, p, remaining);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->length = remaining;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering}
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringint stdout_stream_process(StdoutStream *s) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering ssize_t l;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (l < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (errno == EAGAIN)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_warning("Failed to read from stream: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (l == 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = stdout_stream_scan(s, true);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->length += l;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = stdout_stream_scan(s, false);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 1;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering}
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringvoid stdout_stream_free(StdoutStream *s) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->server) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s->server->n_stdout_streams > 0);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->server->n_stdout_streams --;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->fd >= 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->server)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering close_nointr_nofail(s->fd);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#ifdef HAVE_SELINUX
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->security_context)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering freecon(s->security_context);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#endif
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering free(s->identifier);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering free(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering}
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringint stdout_stream_new(Server *s) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering StdoutStream *stream;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int fd, r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering socklen_t len;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering struct epoll_event ev;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (fd < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (errno == EAGAIN)
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_error("Failed to accept stdout connection: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_warning("Too many stdout streams, refusing connection.");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering close_nointr_nofail(fd);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering stream = new0(StdoutStream, 1);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (!stream) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering close_nointr_nofail(fd);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return log_oom();
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering stream->fd = fd;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering len = sizeof(stream->ucred);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_error("Failed to determine peer credentials: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering goto fail;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#ifdef HAVE_SELINUX
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (use_selinux()) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_error("Failed to determine peer security context: %m");
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#endif
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (shutdown(fd, SHUT_WR) < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_error("Failed to shutdown writing side of socket: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering goto fail;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering zero(ev);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering ev.data.ptr = stream;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering ev.events = EPOLLIN;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_error("Failed to add stream to event loop: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering goto fail;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering stream->server = s;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->n_stdout_streams ++;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringfail:
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering stdout_stream_free(stream);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering}
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poetteringint server_open_stdout_socket(Server *s) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering int r;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering struct epoll_event ev;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering assert(s);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->stdout_fd < 0) {
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek union sockaddr_union sa = {
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek .un.sun_family = AF_UNIX,
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek .un.sun_path = "/run/systemd/journal/stdout",
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek };
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (s->stdout_fd < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_error("socket() failed: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering unlink(sa.un.sun_path);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (r < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_error("bind() failed: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering chmod(sa.un.sun_path, 0666);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (listen(s->stdout_fd, SOMAXCONN) < 0) {
8060a8b32d9f0c6244dfb6814fb0e339475bcd98Auke Kok log_error("listen() failed: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering } else
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering fd_nonblock(s->stdout_fd, 1);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering zero(ev);
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering ev.events = EPOLLIN;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering ev.data.fd = s->stdout_fd;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering log_error("Failed to add stdout server fd to epoll object: %m");
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return -errno;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering }
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering return 0;
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering}