journald-stream.c revision cfa1b98e832026b0fa5f1ca2f8f5f65bddf12a31
/*-*- 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 <stddef.h>
#include <unistd.h>
#ifdef HAVE_SELINUX
#endif
#include "sd-daemon.h"
#include "sd-event.h"
#include "alloc-util.h"
#include "dirent-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "journald-console.h"
#include "journald-kmsg.h"
#include "journald-server.h"
#include "journald-stream.h"
#include "journald-syslog.h"
#include "journald-wall.h"
#include "mkdir.h"
#include "parse-util.h"
#include "selinux-util.h"
#include "socket-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "syslog-util.h"
#define STDOUT_STREAMS_MAX 4096
typedef enum StdoutStreamState {
struct StdoutStream {
int fd;
char *label;
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;
bool fdstore:1;
bool in_notify_queue:1;
char *state_file;
};
void stdout_stream_free(StdoutStream *s) {
if (!s)
return;
if (s->server) {
s->server->n_stdout_streams --;
if (s->in_notify_queue)
}
if (s->event_source) {
}
safe_close(s->fd);
free(s->identifier);
free(s->state_file);
free(s);
}
static void stdout_stream_destroy(StdoutStream *s) {
if (!s)
return;
if (s->state_file)
(void) unlink(s->state_file);
}
static int stdout_stream_save(StdoutStream *s) {
int r;
assert(s);
if (s->state != STDOUT_STREAM_RUNNING)
return 0;
if (!s->state_file) {
if (r < 0)
/* We use device and inode numbers as identifier for the stream */
if (asprintf(&s->state_file, "/run/systemd/journal/streams/%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
return log_oom();
}
if (r < 0)
goto fail;
fprintf(f,
"# This is private data. Do not parse\n"
"PRIORITY=%i\n"
"LEVEL_PREFIX=%i\n"
"FORWARD_TO_SYSLOG=%i\n"
"FORWARD_TO_KMSG=%i\n"
"FORWARD_TO_CONSOLE=%i\n",
s->priority,
s->level_prefix,
s->forward_to_kmsg,
s->forward_to_console);
if (!isempty(s->identifier)) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto fail;
}
}
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto fail;
}
}
r = fflush_and_check(f);
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
if (!s->fdstore && !s->in_notify_queue) {
s->in_notify_queue = true;
if (s->server->notify_event_source) {
if (r < 0)
log_warning_errno(r, "Failed to enable notify event source: %m");
}
}
return 0;
fail:
(void) unlink(s->state_file);
if (temp_path)
}
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 (s->level_prefix)
syslog_parse_priority(&p, &priority, false);
if (isempty(p))
return 0;
server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
if (s->server->forward_to_wall)
if (priority & LOG_FACMASK) {
}
if (s->identifier) {
if (syslog_identifier)
}
if (message)
server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, s->label, label_len, s->unit_id, priority, 0);
return 0;
}
static int stdout_stream_line(StdoutStream *s, char *p) {
int r;
char *orig;
assert(s);
assert(p);
orig = 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;
/* Try to save the stream, so that journald can be restarted and we can recover */
(void) stdout_stream_save(s);
return 0;
case STDOUT_STREAM_RUNNING:
return stdout_stream_log(s, orig);
}
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);
goto terminate;
}
if (l < 0) {
return 0;
goto terminate;
}
if (l == 0) {
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;
}
int r;
assert(s);
if (!stream)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to determine peer credentials: %m");
if (mac_selinux_use()) {
if (r < 0 && r != -EOPNOTSUPP)
(void) log_warning_errno(r, "Failed to determine peer security context: %m");
}
if (r < 0)
return log_error_errno(r, "Failed to add stream to event loop: %m");
if (r < 0)
return log_error_errno(r, "Failed to adjust stdout event source priority: %m");
s->n_stdout_streams ++;
if (ret)
return 0;
}
static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revents, void *userdata) {
int r;
assert(s);
return -EIO;
}
if (fd < 0) {
return 0;
}
if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
log_warning("Too many stdout streams, refusing connection.");
return 0;
}
if (r < 0)
return r;
fd = -1;
return 0;
}
_cleanup_free_ char
*level_prefix = NULL,
*forward_to_kmsg = NULL,
int r;
if (!stream->state_file) {
if (!stream->state_file)
return log_oom();
}
"PRIORITY", &priority,
"LEVEL_PREFIX", &level_prefix,
"FORWARD_TO_SYSLOG", &forward_to_syslog,
"FORWARD_TO_KMSG", &forward_to_kmsg,
"FORWARD_TO_CONSOLE", &forward_to_console,
NULL);
if (r < 0)
if (priority) {
int p;
if (p >= 0)
}
if (level_prefix) {
r = parse_boolean(level_prefix);
if (r >= 0)
stream->level_prefix = r;
}
if (forward_to_syslog) {
if (r >= 0)
stream->forward_to_syslog = r;
}
if (forward_to_kmsg) {
r = parse_boolean(forward_to_kmsg);
if (r >= 0)
stream->forward_to_kmsg = r;
}
if (forward_to_console) {
if (r >= 0)
stream->forward_to_console = r;
}
return 0;
}
int r;
assert(s);
if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
log_warning("Too many stdout streams, refusing restoring of stream.");
return -ENOBUFS;
}
if (r < 0)
return r;
/* Ignore all parsing errors */
return 0;
}
int r;
if (!d) {
return 0;
}
bool found = false;
Iterator i;
int fd;
continue;
found = true;
break;
}
}
if (!found) {
/* No file descriptor? Then let's delete the state file */
continue;
}
if (r < 0)
safe_close(fd);
}
return 0;
fail:
}
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)
r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0)
} else
r = sd_event_add_io(s->event, &s->stdout_event_source, s->stdout_fd, EPOLLIN, stdout_stream_new, s);
if (r < 0)
return log_error_errno(r, "Failed to add stdout server fd to event source: %m");
if (r < 0)
return log_error_errno(r, "Failed to adjust priority of stdout server event source: %m");
return 0;
}
void stdout_stream_send_notify(StdoutStream *s) {
.iov_base = (char*) "FDSTORE=1",
};
.msg_iovlen = 1,
};
ssize_t l;
assert(s);
assert(s->in_notify_queue);
/* Store the connection fd in PID 1, so that we get it passed
* in again on next start */
if (l < 0) {
return;
} else {
log_debug("Successfully sent stream file descriptor to service manager.");
s->fdstore = 1;
}
s->in_notify_queue = false;
}