- iovec[n].iov_base = (void*) p;
- iovec[n].iov_len = l;
- n++;
-
- IOVEC_SET_STRING(iovec[n++], (char*) "\n");
-
- writev(console, iovec, n);
- }
- }
-
- free(message);
- free(syslog_priority);
-
- return 0;
-}
-
-static int stdout_stream_line(StdoutStream *s, const char *p, size_t l) {
- assert(s);
- assert(p);
-
- while (l > 0 && strchr(WHITESPACE, *p)) {
- l--;
- p++;
- }
-
- while (l > 0 && strchr(WHITESPACE, *(p+l-1)))
- l--;
-
- switch (s->state) {
-
- case STDOUT_STREAM_TAG:
-
- if (l > 0) {
- s->tag = strndup(p, l);
- if (!s->tag) {
- log_error("Out of memory");
- return -EINVAL;
- }
- }
-
- s->state = STDOUT_STREAM_PRIORITY;
- return 0;
-
- case STDOUT_STREAM_PRIORITY:
- if (l != 1 || *p < '0' || *p > '7') {
- log_warning("Failed to parse log priority line.");
- return -EINVAL;
- }
-
- s->priority = *p - '0';
- s->state = STDOUT_STREAM_PRIORITY_PREFIX;
- return 0;
-
- case STDOUT_STREAM_PRIORITY_PREFIX:
- if (l != 1 || *p < '0' || *p > '1') {
- log_warning("Failed to parse priority prefix line.");
- return -EINVAL;
- }
-
- s->priority_prefix = *p - '0';
- s->state = STDOUT_STREAM_TEE_CONSOLE;
- return 0;
-
- case STDOUT_STREAM_TEE_CONSOLE:
- if (l != 1 || *p < '0' || *p > '1') {
- log_warning("Failed to parse tee to console line.");
- return -EINVAL;
- }
-
- s->tee_console = *p - '0';
- s->state = STDOUT_STREAM_RUNNING;
- return 0;
-
- case STDOUT_STREAM_RUNNING:
- return stdout_stream_log(s, p, l);
- }
-
- assert_not_reached("Unknown stream state");
-}
-
-static int stdout_stream_scan(StdoutStream *s, bool force_flush) {
- char *p;
- size_t remaining;
- int r;
-
- assert(s);
-
- p = s->buffer;
- remaining = s->length;
- for (;;) {
- char *end;
- size_t skip;
-
- end = memchr(p, '\n', remaining);
- if (!end) {
- if (remaining >= LINE_MAX) {
- end = p + LINE_MAX;
- skip = LINE_MAX;
- } else
- break;
- } else
- skip = end - p + 1;
-
- r = stdout_stream_line(s, p, end - p);
- if (r < 0)
- return r;
-
- remaining -= skip;
- p += skip;
- }
-
- if (force_flush && remaining > 0) {
- r = stdout_stream_line(s, p, remaining);
- if (r < 0)
- return r;
-
- p += remaining;
- remaining = 0;
- }
-
- if (p > s->buffer) {
- memmove(s->buffer, p, remaining);
- s->length = remaining;
- }
-
- return 0;
-}
-
-static int stdout_stream_process(StdoutStream *s) {
- ssize_t l;
- int r;
-
- assert(s);
-
- l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
- if (l < 0) {
-
- if (errno == EAGAIN)
- 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;
-
-}
-
-static void stdout_stream_free(StdoutStream *s) {
- assert(s);
-
- if (s->server) {
- assert(s->server->n_stdout_streams > 0);
- s->server->n_stdout_streams --;
- LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s);
- }
-
- if (s->fd >= 0) {
- if (s->server)
- epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
-
- close_nointr_nofail(s->fd);
- }
-
- free(s->tag);
- free(s);
-}
-
-static int stdout_stream_new(Server *s) {
- StdoutStream *stream;
- int fd, r;
- socklen_t len;
- struct epoll_event ev;
-
- assert(s);
-
- fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
- if (fd < 0) {
- if (errno == EAGAIN)
- 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.");
- close_nointr_nofail(fd);
- return 0;
- }
-
- stream = new0(StdoutStream, 1);
- if (!stream) {
- log_error("Out of memory.");
- close_nointr_nofail(fd);
- return -ENOMEM;
- }
-
- stream->fd = fd;
-
- len = sizeof(stream->ucred);
- if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
- log_error("Failed to determine peer credentials: %m");
- r = -errno;
- goto fail;
- }
-
- if (shutdown(fd, SHUT_WR) < 0) {
- log_error("Failed to shutdown writing side of socket: %m");
- r = -errno;
- goto fail;
- }
-
- zero(ev);
- ev.data.ptr = stream;
- ev.events = EPOLLIN;
- if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
- log_error("Failed to add stream to event loop: %m");
- r = -errno;
- goto fail;
- }
-
- stream->server = s;
- LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream);
- s->n_stdout_streams ++;
-
- return 0;
-
-fail:
- stdout_stream_free(stream);
- return r;
-}
-
-static int system_journal_open(Server *s) {
- int r;
- char *fn;
- sd_id128_t machine;
- char ids[33];
-
- r = sd_id128_get_machine(&machine);
- if (r < 0)
- return r;
-
- sd_id128_to_string(machine, ids);
-
- if (!s->system_journal) {
-
- /* First try to create the machine path, but not the prefix */
- fn = strappend("/var/log/journal/", ids);
- if (!fn)
- return -ENOMEM;
- (void) mkdir(fn, 0755);
- free(fn);
-
- /* The create the system journal file */
- fn = join("/var/log/journal/", ids, "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
-
- r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
- free(fn);
-
- if (r >= 0) {
- journal_default_metrics(&s->system_metrics, s->system_journal->fd);
-
- s->system_journal->metrics = s->system_metrics;
- s->system_journal->compress = s->compress;
-
- fix_perms(s->system_journal, 0);
- } else if (r < 0) {
-
- if (r != -ENOENT && r != -EROFS)
- log_warning("Failed to open system journal: %s", strerror(-r));
-
- r = 0;
- }
- }
-
- if (!s->runtime_journal) {
-
- fn = join("/run/log/journal/", ids, "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
-
- if (s->system_journal) {
-
- /* Try to open the runtime journal, but only
- * if it already exists, so that we can flush
- * it into the system journal */
-
- r = journal_file_open(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
- free(fn);
-
- if (r < 0) {
- if (r != -ENOENT)
- log_warning("Failed to open runtime journal: %s", strerror(-r));
-
- r = 0;
- }
-
- } else {
-
- /* OK, we really need the runtime journal, so create
- * it if necessary. */
-
- (void) mkdir_parents(fn, 0755);
- r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
- free(fn);
-
- if (r < 0) {
- log_error("Failed to open runtime journal: %s", strerror(-r));
- return r;
- }
- }
-
- if (s->runtime_journal) {
- journal_default_metrics(&s->runtime_metrics, s->runtime_journal->fd);
-
- s->runtime_journal->metrics = s->runtime_metrics;
- s->runtime_journal->compress = s->compress;
-
- fix_perms(s->runtime_journal, 0);
- }
- }
-
- return r;
-}
-
-static int server_flush_to_var(Server *s) {
- char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
- Object *o = NULL;
- int r;
- sd_id128_t machine;
- sd_journal *j;
- usec_t ts;
-
- assert(s);
-
- if (!s->runtime_journal)
- return 0;
-
- ts = now(CLOCK_MONOTONIC);
- if (s->var_available_timestamp + RECHECK_VAR_AVAILABLE_USEC > ts)
- return 0;
-
- s->var_available_timestamp = ts;
-
- system_journal_open(s);
-
- if (!s->system_journal)
- return 0;
-
- r = sd_id128_get_machine(&machine);
- if (r < 0) {
- log_error("Failed to get machine id: %s", strerror(-r));
- return r;
- }
-
- r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
- if (r < 0) {
- log_error("Failed to read runtime journal: %s", strerror(-r));
- return r;
- }
-
- SD_JOURNAL_FOREACH(j) {
- JournalFile *f;
-
- f = j->current_file;
- assert(f && f->current_offset > 0);
-
- r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
- if (r < 0) {
- log_error("Can't read entry: %s", strerror(-r));
- goto finish;
- }