journald-stream.c revision 91bf3b3e124575f6f647bff29766e9d992f55b32
/*-*- 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 "sd-event.h"
#include "socket-util.h"
#include "selinux-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) {
int priority;
char syslog_priority[] = "PRIORITY=\0";
unsigned n = 0;
assert(s);
assert(p);
if (isempty(p))
return 0;
if (s->level_prefix)
syslog_parse_priority(&p, &priority, false);
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, 0);
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;
}
StdoutStream *s = userdata;
ssize_t l;
int r;
assert(s);
r = -EIO;
goto terminate;
}
if (l < 0) {
return 0;
log_warning("Failed to read from stream: %m");
r = -errno;
goto terminate;
}
if (l == 0) {
r = stdout_stream_scan(s, true);
goto terminate;
}
s->length += l;
r = stdout_stream_scan(s, false);
if (r < 0)
goto terminate;
return 1;
return 0;
}
void stdout_stream_free(StdoutStream *s) {
assert(s);
if (s->server) {
s->server->n_stdout_streams --;
}
if (s->event_source)
if (s->fd >= 0)
close_nointr_nofail(s->fd);
#ifdef HAVE_SELINUX
if (s->security_context)
freecon(s->security_context);
#endif
free(s->identifier);
free(s);
}
static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revents, void *userdata) {
int fd, r;
assert(s);
return -EIO;
}
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
if (use_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;
}
if (r < 0) {
goto fail;
}
if (r < 0) {
goto fail;
}
s->n_stdout_streams ++;
return 0;
fail:
return r;
}
int server_open_stdout_socket(Server *s) {
int r;
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
r = sd_event_add_io(s->event, s->stdout_fd, EPOLLIN, stdout_stream_new, s, &s->stdout_event_source);
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
return 0;
}