journald-stream.c revision 8060a8b32d9f0c6244dfb6814fb0e339475bcd98
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
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 <fcntl.h>
#include <unistd.h>
#include <stddef.h>
#ifdef HAVE_SELINUX
#endif
#include "socket-util.h"
#include "journald-server.h"
#include "journald-stream.h"
#include "journald-syslog.h"
#include "journald-kmsg.h"
#include "journald-console.h"
#define STDOUT_STREAMS_MAX 4096
typedef enum StdoutStreamState {
struct StdoutStream {
int fd;
#ifdef HAVE_SELINUX
#endif
char *identifier;
char *unit_id;
int priority;
bool level_prefix:1;
bool forward_to_syslog:1;
bool forward_to_kmsg:1;
bool forward_to_console:1;
};
static int stdout_stream_log(StdoutStream *s, const char *p) {
unsigned n = 0;
int priority;
assert(s);
assert(p);
if (isempty(p))
return 0;
if (s->level_prefix)
syslog_parse_priority((char**) &p, &priority);
server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
if (priority & LOG_FACMASK)
if (s->identifier) {
if (syslog_identifier)
}
if (message)
#ifdef HAVE_SELINUX
if (s->security_context) {
label = (char*) s->security_context;
}
#endif
server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority);
return 0;
}
static int stdout_stream_line(StdoutStream *s, char *p) {
int r;
assert(s);
assert(p);
p = strstrip(p);
switch (s->state) {
case STDOUT_STREAM_IDENTIFIER:
if (isempty(p))
s->identifier = NULL;
else {
s->identifier = strdup(p);
if (!s->identifier)
return log_oom();
}
s->state = STDOUT_STREAM_UNIT_ID;
return 0;
case STDOUT_STREAM_UNIT_ID:
if (isempty(p))
else {
if (!s->unit_id)
return log_oom();
}
}
s->state = STDOUT_STREAM_PRIORITY;
return 0;
case STDOUT_STREAM_PRIORITY:
log_warning("Failed to parse log priority line.");
return -EINVAL;
}
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse level prefix line.");
return -EINVAL;
}
s->level_prefix = !!r;
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse forward to syslog line.");
return -EINVAL;
}
s->forward_to_syslog = !!r;
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse copy to kmsg line.");
return -EINVAL;
}
s->forward_to_kmsg = !!r;
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse copy to console line.");
return -EINVAL;
}
s->forward_to_console = !!r;
s->state = STDOUT_STREAM_RUNNING;
return 0;
case STDOUT_STREAM_RUNNING:
return stdout_stream_log(s, p);
}
assert_not_reached("Unknown stream state");
}
char *p;
int r;
assert(s);
p = s->buffer;
for (;;) {
char *end;
if (end)
} else
break;
*end = 0;
r = stdout_stream_line(s, p);
if (r < 0)
return r;
p += skip;
}
if (force_flush && remaining > 0) {
p[remaining] = 0;
r = stdout_stream_line(s, p);
if (r < 0)
return r;
p += remaining;
remaining = 0;
}
if (p > s->buffer) {
}
return 0;
}
int stdout_stream_process(StdoutStream *s) {
ssize_t l;
int r;
assert(s);
if (l < 0) {
return 0;
log_warning("Failed to read from stream: %m");
return -errno;
}
if (l == 0) {
r = stdout_stream_scan(s, true);
if (r < 0)
return r;
return 0;
}
s->length += l;
r = stdout_stream_scan(s, false);
if (r < 0)
return r;
return 1;
}
void stdout_stream_free(StdoutStream *s) {
assert(s);
if (s->server) {
s->server->n_stdout_streams --;
}
if (s->fd >= 0) {
if (s->server)
close_nointr_nofail(s->fd);
}
#ifdef HAVE_SELINUX
if (s->security_context)
freecon(s->security_context);
#endif
free(s->identifier);
free(s);
}
int stdout_stream_new(Server *s) {
int fd, r;
struct epoll_event ev;
assert(s);
if (fd < 0) {
return 0;
log_error("Failed to accept stdout connection: %m");
return -errno;
}
if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
log_warning("Too many stdout streams, refusing connection.");
return 0;
}
if (!stream) {
return log_oom();
}
log_error("Failed to determine peer credentials: %m");
r = -errno;
goto fail;
}
#ifdef HAVE_SELINUX
log_error("Failed to determine peer security context: %m");
#endif
log_error("Failed to shutdown writing side of socket: %m");
r = -errno;
goto fail;
}
log_error("Failed to add stream to event loop: %m");
r = -errno;
goto fail;
}
s->n_stdout_streams ++;
return 0;
fail:
return r;
}
int server_open_stdout_socket(Server *s) {
int r;
struct epoll_event ev;
assert(s);
if (s->stdout_fd < 0) {
union sockaddr_union sa = {
};
if (s->stdout_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
r = bind(s->stdout_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;
}
log_error("listen() failed: %m");
return -errno;
}
} else
log_error("Failed to add stdout server fd to epoll object: %m");
return -errno;
}
return 0;
}