]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald-stream.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / journal / journald-stream.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a45b9fca
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2011 Lennart Poettering
a45b9fca
LP
6***/
7
4871690d 8#include <stddef.h>
07630cea 9#include <unistd.h>
a45b9fca 10
349cc4a5 11#if HAVE_SELINUX
a45b9fca
LP
12#include <selinux/selinux.h>
13#endif
14
13790add 15#include "sd-daemon.h"
4f5dd394
LP
16#include "sd-event.h"
17
b5efdb8a 18#include "alloc-util.h"
a0956174 19#include "dirent-util.h"
4f5dd394 20#include "escape.h"
3ffd4af2 21#include "fd-util.h"
13790add 22#include "fileio.h"
afc5dbf3 23#include "io-util.h"
4f5dd394 24#include "journald-console.h"
22e3a02b 25#include "journald-context.h"
4f5dd394 26#include "journald-kmsg.h"
d025f1e4 27#include "journald-server.h"
3ffd4af2 28#include "journald-stream.h"
a45b9fca 29#include "journald-syslog.h"
40b71e89 30#include "journald-wall.h"
4f5dd394 31#include "mkdir.h"
6bedfcbb 32#include "parse-util.h"
22e3a02b 33#include "process-util.h"
4f5dd394
LP
34#include "selinux-util.h"
35#include "socket-util.h"
15a5e950 36#include "stdio-util.h"
07630cea 37#include "string-util.h"
7ccbd1ae 38#include "syslog-util.h"
7a1f1aaa 39#include "unit-name.h"
a45b9fca
LP
40
41#define STDOUT_STREAMS_MAX 4096
42
43typedef enum StdoutStreamState {
44 STDOUT_STREAM_IDENTIFIER,
45 STDOUT_STREAM_UNIT_ID,
46 STDOUT_STREAM_PRIORITY,
47 STDOUT_STREAM_LEVEL_PREFIX,
48 STDOUT_STREAM_FORWARD_TO_SYSLOG,
49 STDOUT_STREAM_FORWARD_TO_KMSG,
50 STDOUT_STREAM_FORWARD_TO_CONSOLE,
51 STDOUT_STREAM_RUNNING
52} StdoutStreamState;
53
ec20fe5f
LP
54/* The different types of log record terminators: a real \n was read, a NUL character was read, the maximum line length
55 * was reached, or the end of the stream was reached */
56
57typedef enum LineBreak {
58 LINE_BREAK_NEWLINE,
59 LINE_BREAK_NUL,
60 LINE_BREAK_LINE_MAX,
61 LINE_BREAK_EOF,
62} LineBreak;
63
a45b9fca
LP
64struct StdoutStream {
65 Server *server;
66 StdoutStreamState state;
67
68 int fd;
69
70 struct ucred ucred;
2de56f70 71 char *label;
a45b9fca
LP
72 char *identifier;
73 char *unit_id;
74 int priority;
75 bool level_prefix:1;
76 bool forward_to_syslog:1;
77 bool forward_to_kmsg:1;
78 bool forward_to_console:1;
79
13790add 80 bool fdstore:1;
e22aa3d3 81 bool in_notify_queue:1;
13790add 82
ec20fe5f 83 char *buffer;
a45b9fca 84 size_t length;
ec20fe5f 85 size_t allocated;
a45b9fca 86
f9a810be
LP
87 sd_event_source *event_source;
88
13790add
LP
89 char *state_file;
90
22e3a02b
LP
91 ClientContext *context;
92
a45b9fca 93 LIST_FIELDS(StdoutStream, stdout_stream);
e22aa3d3 94 LIST_FIELDS(StdoutStream, stdout_stream_notify_queue);
ec20fe5f 95
fbd0b64f 96 char id_field[STRLEN("_STREAM_ID=") + SD_ID128_STRING_MAX];
a45b9fca
LP
97};
98
13790add
LP
99void stdout_stream_free(StdoutStream *s) {
100 if (!s)
101 return;
102
103 if (s->server) {
22e3a02b
LP
104
105 if (s->context)
106 client_context_release(s->server, s->context);
107
13790add 108 assert(s->server->n_stdout_streams > 0);
313cefa1 109 s->server->n_stdout_streams--;
13790add 110 LIST_REMOVE(stdout_stream, s->server->stdout_streams, s);
e22aa3d3
LP
111
112 if (s->in_notify_queue)
113 LIST_REMOVE(stdout_stream_notify_queue, s->server->stdout_streams_notify_queue, s);
13790add
LP
114 }
115
116 if (s->event_source) {
117 sd_event_source_set_enabled(s->event_source, SD_EVENT_OFF);
118 s->event_source = sd_event_source_unref(s->event_source);
119 }
120
121 safe_close(s->fd);
2de56f70 122 free(s->label);
13790add
LP
123 free(s->identifier);
124 free(s->unit_id);
125 free(s->state_file);
ec20fe5f 126 free(s->buffer);
13790add
LP
127
128 free(s);
129}
130
131DEFINE_TRIVIAL_CLEANUP_FUNC(StdoutStream*, stdout_stream_free);
132
133static void stdout_stream_destroy(StdoutStream *s) {
134 if (!s)
135 return;
136
137 if (s->state_file)
e22aa3d3 138 (void) unlink(s->state_file);
13790add
LP
139
140 stdout_stream_free(s);
141}
142
143static int stdout_stream_save(StdoutStream *s) {
144 _cleanup_free_ char *temp_path = NULL;
145 _cleanup_fclose_ FILE *f = NULL;
146 int r;
147
148 assert(s);
149
150 if (s->state != STDOUT_STREAM_RUNNING)
151 return 0;
152
153 if (!s->state_file) {
154 struct stat st;
155
156 r = fstat(s->fd, &st);
157 if (r < 0)
158 return log_warning_errno(errno, "Failed to stat connected stream: %m");
159
160 /* We use device and inode numbers as identifier for the stream */
161 if (asprintf(&s->state_file, "/run/systemd/journal/streams/%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
162 return log_oom();
163 }
164
165 mkdir_p("/run/systemd/journal/streams", 0755);
166
167 r = fopen_temporary(s->state_file, &f, &temp_path);
168 if (r < 0)
dacd6cee 169 goto fail;
13790add
LP
170
171 fprintf(f,
172 "# This is private data. Do not parse\n"
173 "PRIORITY=%i\n"
174 "LEVEL_PREFIX=%i\n"
175 "FORWARD_TO_SYSLOG=%i\n"
176 "FORWARD_TO_KMSG=%i\n"
ec20fe5f
LP
177 "FORWARD_TO_CONSOLE=%i\n"
178 "STREAM_ID=%s\n",
13790add
LP
179 s->priority,
180 s->level_prefix,
181 s->forward_to_syslog,
182 s->forward_to_kmsg,
ec20fe5f 183 s->forward_to_console,
fbd0b64f 184 s->id_field + STRLEN("_STREAM_ID="));
13790add
LP
185
186 if (!isempty(s->identifier)) {
187 _cleanup_free_ char *escaped;
188
189 escaped = cescape(s->identifier);
190 if (!escaped) {
191 r = -ENOMEM;
dacd6cee 192 goto fail;
13790add
LP
193 }
194
195 fprintf(f, "IDENTIFIER=%s\n", escaped);
196 }
197
198 if (!isempty(s->unit_id)) {
199 _cleanup_free_ char *escaped;
200
201 escaped = cescape(s->unit_id);
202 if (!escaped) {
203 r = -ENOMEM;
dacd6cee 204 goto fail;
13790add
LP
205 }
206
207 fprintf(f, "UNIT=%s\n", escaped);
208 }
209
210 r = fflush_and_check(f);
211 if (r < 0)
dacd6cee 212 goto fail;
13790add
LP
213
214 if (rename(temp_path, s->state_file) < 0) {
215 r = -errno;
dacd6cee 216 goto fail;
13790add
LP
217 }
218
e22aa3d3
LP
219 if (!s->fdstore && !s->in_notify_queue) {
220 LIST_PREPEND(stdout_stream_notify_queue, s->server->stdout_streams_notify_queue, s);
221 s->in_notify_queue = true;
222
223 if (s->server->notify_event_source) {
224 r = sd_event_source_set_enabled(s->server->notify_event_source, SD_EVENT_ON);
225 if (r < 0)
226 log_warning_errno(r, "Failed to enable notify event source: %m");
227 }
13790add
LP
228 }
229
dacd6cee 230 return 0;
13790add 231
dacd6cee
LP
232fail:
233 (void) unlink(s->state_file);
234
235 if (temp_path)
236 (void) unlink(temp_path);
13790add 237
dacd6cee 238 return log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
13790add
LP
239}
240
ec20fe5f 241static int stdout_stream_log(StdoutStream *s, const char *p, LineBreak line_break) {
d3070fbd 242 struct iovec *iovec;
a45b9fca 243 int priority;
e3bfb7be 244 char syslog_priority[] = "PRIORITY=\0";
fbd0b64f 245 char syslog_facility[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int) + 1];
e3bfb7be 246 _cleanup_free_ char *message = NULL, *syslog_identifier = NULL;
d3070fbd 247 size_t n = 0, m;
22e3a02b 248 int r;
a45b9fca
LP
249
250 assert(s);
251 assert(p);
252
d3070fbd
LP
253 if (s->context)
254 (void) client_context_maybe_refresh(s->server, s->context, NULL, NULL, 0, NULL, USEC_INFINITY);
255 else if (pid_is_valid(s->ucred.pid)) {
256 r = client_context_acquire(s->server, s->ucred.pid, &s->ucred, s->label, strlen_ptr(s->label), s->unit_id, &s->context);
257 if (r < 0)
258 log_warning_errno(r, "Failed to acquire client context, ignoring: %m");
259 }
260
a45b9fca
LP
261 priority = s->priority;
262
263 if (s->level_prefix)
e3bfb7be 264 syslog_parse_priority(&p, &priority, false);
a45b9fca 265
d3070fbd
LP
266 if (!client_context_test_priority(s->context, priority))
267 return 0;
268
6f526243
EV
269 if (isempty(p))
270 return 0;
271
a45b9fca
LP
272 if (s->forward_to_syslog || s->server->forward_to_syslog)
273 server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL);
274
275 if (s->forward_to_kmsg || s->server->forward_to_kmsg)
276 server_forward_kmsg(s->server, priority, s->identifier, p, &s->ucred);
277
278 if (s->forward_to_console || s->server->forward_to_console)
279 server_forward_console(s->server, priority, s->identifier, p, &s->ucred);
280
40b71e89
ST
281 if (s->server->forward_to_wall)
282 server_forward_wall(s->server, priority, s->identifier, p, &s->ucred);
283
d3070fbd
LP
284 m = N_IOVEC_META_FIELDS + 7 + client_context_extra_fields_n_iovec(s->context);
285 iovec = newa(struct iovec, m);
286
e6a7ec4b
LP
287 iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=stdout");
288 iovec[n++] = IOVEC_MAKE_STRING(s->id_field);
ec20fe5f 289
fbd0b64f 290 syslog_priority[STRLEN("PRIORITY=")] = '0' + LOG_PRI(priority);
e6a7ec4b 291 iovec[n++] = IOVEC_MAKE_STRING(syslog_priority);
a45b9fca 292
e3bfb7be 293 if (priority & LOG_FACMASK) {
5ffa8c81 294 xsprintf(syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority));
e6a7ec4b 295 iovec[n++] = IOVEC_MAKE_STRING(syslog_facility);
e3bfb7be 296 }
a45b9fca
LP
297
298 if (s->identifier) {
299 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
300 if (syslog_identifier)
e6a7ec4b 301 iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier);
a45b9fca
LP
302 }
303
ec20fe5f
LP
304 if (line_break != LINE_BREAK_NEWLINE) {
305 const char *c;
306
307 /* If this log message was generated due to an uncommon line break then mention this in the log
308 * entry */
309
310 c = line_break == LINE_BREAK_NUL ? "_LINE_BREAK=nul" :
311 line_break == LINE_BREAK_LINE_MAX ? "_LINE_BREAK=line-max" :
312 "_LINE_BREAK=eof";
e6a7ec4b 313 iovec[n++] = IOVEC_MAKE_STRING(c);
ec20fe5f
LP
314 }
315
a45b9fca
LP
316 message = strappend("MESSAGE=", p);
317 if (message)
e6a7ec4b 318 iovec[n++] = IOVEC_MAKE_STRING(message);
a45b9fca 319
d3070fbd 320 server_dispatch_message(s->server, iovec, n, m, s->context, NULL, priority, 0);
a45b9fca
LP
321 return 0;
322}
323
ec20fe5f 324static int stdout_stream_line(StdoutStream *s, char *p, LineBreak line_break) {
a45b9fca 325 int r;
cfa1b98e 326 char *orig;
a45b9fca
LP
327
328 assert(s);
329 assert(p);
330
cfa1b98e 331 orig = p;
a45b9fca
LP
332 p = strstrip(p);
333
ec20fe5f
LP
334 /* line breaks by NUL, line max length or EOF are not permissible during the negotiation part of the protocol */
335 if (line_break != LINE_BREAK_NEWLINE && s->state != STDOUT_STREAM_RUNNING) {
336 log_warning("Control protocol line not properly terminated.");
337 return -EINVAL;
338 }
339
a45b9fca
LP
340 switch (s->state) {
341
342 case STDOUT_STREAM_IDENTIFIER:
7a1f1aaa 343 if (!isempty(p)) {
a45b9fca
LP
344 s->identifier = strdup(p);
345 if (!s->identifier)
346 return log_oom();
347 }
348
349 s->state = STDOUT_STREAM_UNIT_ID;
350 return 0;
351
352 case STDOUT_STREAM_UNIT_ID:
7a1f1aaa
LP
353 if (s->ucred.uid == 0 &&
354 unit_name_is_valid(p, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
355
356 s->unit_id = strdup(p);
357 if (!s->unit_id)
358 return log_oom();
a45b9fca
LP
359 }
360
361 s->state = STDOUT_STREAM_PRIORITY;
362 return 0;
363
364 case STDOUT_STREAM_PRIORITY:
365 r = safe_atoi(p, &s->priority);
41891700 366 if (r < 0 || s->priority < 0 || s->priority > 999) {
a45b9fca
LP
367 log_warning("Failed to parse log priority line.");
368 return -EINVAL;
369 }
370
371 s->state = STDOUT_STREAM_LEVEL_PREFIX;
372 return 0;
373
374 case STDOUT_STREAM_LEVEL_PREFIX:
375 r = parse_boolean(p);
376 if (r < 0) {
377 log_warning("Failed to parse level prefix line.");
378 return -EINVAL;
379 }
380
381 s->level_prefix = !!r;
382 s->state = STDOUT_STREAM_FORWARD_TO_SYSLOG;
383 return 0;
384
385 case STDOUT_STREAM_FORWARD_TO_SYSLOG:
386 r = parse_boolean(p);
387 if (r < 0) {
388 log_warning("Failed to parse forward to syslog line.");
389 return -EINVAL;
390 }
391
392 s->forward_to_syslog = !!r;
393 s->state = STDOUT_STREAM_FORWARD_TO_KMSG;
394 return 0;
395
396 case STDOUT_STREAM_FORWARD_TO_KMSG:
397 r = parse_boolean(p);
398 if (r < 0) {
399 log_warning("Failed to parse copy to kmsg line.");
400 return -EINVAL;
401 }
402
403 s->forward_to_kmsg = !!r;
404 s->state = STDOUT_STREAM_FORWARD_TO_CONSOLE;
405 return 0;
406
407 case STDOUT_STREAM_FORWARD_TO_CONSOLE:
408 r = parse_boolean(p);
409 if (r < 0) {
410 log_warning("Failed to parse copy to console line.");
411 return -EINVAL;
412 }
413
414 s->forward_to_console = !!r;
415 s->state = STDOUT_STREAM_RUNNING;
13790add
LP
416
417 /* Try to save the stream, so that journald can be restarted and we can recover */
418 (void) stdout_stream_save(s);
a45b9fca
LP
419 return 0;
420
421 case STDOUT_STREAM_RUNNING:
ec20fe5f 422 return stdout_stream_log(s, orig, line_break);
a45b9fca
LP
423 }
424
425 assert_not_reached("Unknown stream state");
426}
427
428static int stdout_stream_scan(StdoutStream *s, bool force_flush) {
429 char *p;
430 size_t remaining;
431 int r;
432
433 assert(s);
434
435 p = s->buffer;
436 remaining = s->length;
95cbb83c
VC
437
438 /* XXX: This function does nothing if (s->length == 0) */
439
a45b9fca 440 for (;;) {
ec20fe5f 441 LineBreak line_break;
a45b9fca 442 size_t skip;
ec20fe5f
LP
443 char *end1, *end2;
444
445 end1 = memchr(p, '\n', remaining);
446 end2 = memchr(p, 0, end1 ? (size_t) (end1 - p) : remaining);
447
448 if (end2) {
449 /* We found a NUL terminator */
450 skip = end2 - p + 1;
451 line_break = LINE_BREAK_NUL;
452 } else if (end1) {
453 /* We found a \n terminator */
454 *end1 = 0;
455 skip = end1 - p + 1;
456 line_break = LINE_BREAK_NEWLINE;
457 } else if (remaining >= s->server->line_max) {
458 /* Force a line break after the maximum line length */
459 *(p + s->server->line_max) = 0;
a45b9fca 460 skip = remaining;
ec20fe5f 461 line_break = LINE_BREAK_LINE_MAX;
a45b9fca
LP
462 } else
463 break;
464
ec20fe5f 465 r = stdout_stream_line(s, p, line_break);
a45b9fca
LP
466 if (r < 0)
467 return r;
468
469 remaining -= skip;
470 p += skip;
471 }
472
473 if (force_flush && remaining > 0) {
474 p[remaining] = 0;
ec20fe5f 475 r = stdout_stream_line(s, p, LINE_BREAK_EOF);
a45b9fca
LP
476 if (r < 0)
477 return r;
478
479 p += remaining;
480 remaining = 0;
481 }
482
483 if (p > s->buffer) {
484 memmove(s->buffer, p, remaining);
485 s->length = remaining;
486 }
487
488 return 0;
489}
490
f9a810be
LP
491static int stdout_stream_process(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
492 StdoutStream *s = userdata;
ec20fe5f 493 size_t limit;
a45b9fca
LP
494 ssize_t l;
495 int r;
496
497 assert(s);
498
f9a810be
LP
499 if ((revents|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
500 log_error("Got invalid event from epoll for stdout stream: %"PRIx32, revents);
91bf3b3e 501 goto terminate;
f9a810be
LP
502 }
503
ec20fe5f
LP
504 /* If the buffer is full already (discounting the extra NUL we need), add room for another 1K */
505 if (s->length + 1 >= s->allocated) {
506 if (!GREEDY_REALLOC(s->buffer, s->allocated, s->length + 1 + 1024)) {
507 log_oom();
508 goto terminate;
509 }
510 }
a45b9fca 511
ec20fe5f
LP
512 /* Try to make use of the allocated buffer in full, but never read more than the configured line size. Also,
513 * always leave room for a terminating NUL we might need to add. */
514 limit = MIN(s->allocated - 1, s->server->line_max);
515
516 l = read(s->fd, s->buffer + s->length, limit - s->length);
517 if (l < 0) {
a45b9fca
LP
518 if (errno == EAGAIN)
519 return 0;
520
56f64d95 521 log_warning_errno(errno, "Failed to read from stream: %m");
91bf3b3e 522 goto terminate;
a45b9fca
LP
523 }
524
525 if (l == 0) {
7b77ed8c 526 stdout_stream_scan(s, true);
91bf3b3e 527 goto terminate;
a45b9fca
LP
528 }
529
530 s->length += l;
531 r = stdout_stream_scan(s, false);
532 if (r < 0)
91bf3b3e 533 goto terminate;
a45b9fca
LP
534
535 return 1;
536
91bf3b3e 537terminate:
13790add 538 stdout_stream_destroy(s);
f9a810be 539 return 0;
a45b9fca
LP
540}
541
13790add
LP
542static int stdout_stream_install(Server *s, int fd, StdoutStream **ret) {
543 _cleanup_(stdout_stream_freep) StdoutStream *stream = NULL;
ec20fe5f 544 sd_id128_t id;
13790add
LP
545 int r;
546
a45b9fca 547 assert(s);
13790add 548 assert(fd >= 0);
a45b9fca 549
ec20fe5f
LP
550 r = sd_id128_randomize(&id);
551 if (r < 0)
552 return log_error_errno(r, "Failed to generate stream ID: %m");
553
13790add
LP
554 stream = new0(StdoutStream, 1);
555 if (!stream)
556 return log_oom();
a45b9fca 557
13790add
LP
558 stream->fd = -1;
559 stream->priority = LOG_INFO;
a45b9fca 560
ec20fe5f
LP
561 xsprintf(stream->id_field, "_STREAM_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
562
13790add
LP
563 r = getpeercred(fd, &stream->ucred);
564 if (r < 0)
565 return log_error_errno(r, "Failed to determine peer credentials: %m");
a45b9fca 566
6d395665 567 if (mac_selinux_use()) {
2de56f70
ZJS
568 r = getpeersec(fd, &stream->label);
569 if (r < 0 && r != -EOPNOTSUPP)
570 (void) log_warning_errno(r, "Failed to determine peer security context: %m");
13790add 571 }
a45b9fca 572
13790add
LP
573 (void) shutdown(fd, SHUT_WR);
574
575 r = sd_event_add_io(s->event, &stream->event_source, fd, EPOLLIN, stdout_stream_process, stream);
576 if (r < 0)
577 return log_error_errno(r, "Failed to add stream to event loop: %m");
578
579 r = sd_event_source_set_priority(stream->event_source, SD_EVENT_PRIORITY_NORMAL+5);
580 if (r < 0)
581 return log_error_errno(r, "Failed to adjust stdout event source priority: %m");
582
583 stream->fd = fd;
584
585 stream->server = s;
586 LIST_PREPEND(stdout_stream, s->stdout_streams, stream);
313cefa1 587 s->n_stdout_streams++;
13790add
LP
588
589 if (ret)
590 *ret = stream;
591
592 stream = NULL;
593
594 return 0;
a45b9fca
LP
595}
596
f9a810be 597static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revents, void *userdata) {
13790add 598 _cleanup_close_ int fd = -1;
f9a810be 599 Server *s = userdata;
13790add 600 int r;
a45b9fca
LP
601
602 assert(s);
603
f9a810be
LP
604 if (revents != EPOLLIN) {
605 log_error("Got invalid event from epoll for stdout server fd: %"PRIx32, revents);
606 return -EIO;
607 }
608
a45b9fca
LP
609 fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
610 if (fd < 0) {
611 if (errno == EAGAIN)
612 return 0;
613
e1427b13 614 return log_error_errno(errno, "Failed to accept stdout connection: %m");
a45b9fca
LP
615 }
616
617 if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
618 log_warning("Too many stdout streams, refusing connection.");
a45b9fca
LP
619 return 0;
620 }
621
13790add
LP
622 r = stdout_stream_install(s, fd, NULL);
623 if (r < 0)
624 return r;
625
626 fd = -1;
627 return 0;
628}
629
630static int stdout_stream_load(StdoutStream *stream, const char *fname) {
631 _cleanup_free_ char
632 *priority = NULL,
633 *level_prefix = NULL,
634 *forward_to_syslog = NULL,
635 *forward_to_kmsg = NULL,
ec20fe5f
LP
636 *forward_to_console = NULL,
637 *stream_id = NULL;
13790add
LP
638 int r;
639
640 assert(stream);
641 assert(fname);
642
643 if (!stream->state_file) {
644 stream->state_file = strappend("/run/systemd/journal/streams/", fname);
645 if (!stream->state_file)
646 return log_oom();
a45b9fca
LP
647 }
648
13790add
LP
649 r = parse_env_file(stream->state_file, NEWLINE,
650 "PRIORITY", &priority,
651 "LEVEL_PREFIX", &level_prefix,
652 "FORWARD_TO_SYSLOG", &forward_to_syslog,
653 "FORWARD_TO_KMSG", &forward_to_kmsg,
654 "FORWARD_TO_CONSOLE", &forward_to_console,
655 "IDENTIFIER", &stream->identifier,
656 "UNIT", &stream->unit_id,
ec20fe5f 657 "STREAM_ID", &stream_id,
13790add
LP
658 NULL);
659 if (r < 0)
660 return log_error_errno(r, "Failed to read: %s", stream->state_file);
a45b9fca 661
13790add
LP
662 if (priority) {
663 int p;
664
665 p = log_level_from_string(priority);
666 if (p >= 0)
667 stream->priority = p;
a45b9fca
LP
668 }
669
13790add
LP
670 if (level_prefix) {
671 r = parse_boolean(level_prefix);
672 if (r >= 0)
673 stream->level_prefix = r;
d682b3a7 674 }
a45b9fca 675
13790add
LP
676 if (forward_to_syslog) {
677 r = parse_boolean(forward_to_syslog);
678 if (r >= 0)
679 stream->forward_to_syslog = r;
a45b9fca
LP
680 }
681
13790add
LP
682 if (forward_to_kmsg) {
683 r = parse_boolean(forward_to_kmsg);
684 if (r >= 0)
685 stream->forward_to_kmsg = r;
f9a810be
LP
686 }
687
13790add
LP
688 if (forward_to_console) {
689 r = parse_boolean(forward_to_console);
690 if (r >= 0)
691 stream->forward_to_console = r;
a45b9fca
LP
692 }
693
ec20fe5f
LP
694 if (stream_id) {
695 sd_id128_t id;
696
697 r = sd_id128_from_string(stream_id, &id);
698 if (r >= 0)
699 xsprintf(stream->id_field, "_STREAM_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
700 }
701
13790add
LP
702 return 0;
703}
704
705static int stdout_stream_restore(Server *s, const char *fname, int fd) {
706 StdoutStream *stream;
707 int r;
708
709 assert(s);
710 assert(fname);
711 assert(fd >= 0);
712
713 if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
714 log_warning("Too many stdout streams, refusing restoring of stream.");
715 return -ENOBUFS;
716 }
717
718 r = stdout_stream_install(s, fd, &stream);
719 if (r < 0)
720 return r;
721
722 stream->state = STDOUT_STREAM_RUNNING;
723 stream->fdstore = true;
724
725 /* Ignore all parsing errors */
726 (void) stdout_stream_load(stream, fname);
a45b9fca
LP
727
728 return 0;
13790add
LP
729}
730
15d91bff 731int server_restore_streams(Server *s, FDSet *fds) {
13790add
LP
732 _cleanup_closedir_ DIR *d = NULL;
733 struct dirent *de;
734 int r;
735
736 d = opendir("/run/systemd/journal/streams");
737 if (!d) {
738 if (errno == ENOENT)
739 return 0;
740
741 return log_warning_errno(errno, "Failed to enumerate /run/systemd/journal/streams: %m");
742 }
743
744 FOREACH_DIRENT(de, d, goto fail) {
745 unsigned long st_dev, st_ino;
746 bool found = false;
747 Iterator i;
748 int fd;
749
750 if (sscanf(de->d_name, "%lu:%lu", &st_dev, &st_ino) != 2)
751 continue;
752
753 FDSET_FOREACH(fd, fds, i) {
754 struct stat st;
755
756 if (fstat(fd, &st) < 0)
757 return log_error_errno(errno, "Failed to stat %s: %m", de->d_name);
758
759 if (S_ISSOCK(st.st_mode) && st.st_dev == st_dev && st.st_ino == st_ino) {
760 found = true;
761 break;
762 }
763 }
764
765 if (!found) {
766 /* No file descriptor? Then let's delete the state file */
767 log_debug("Cannot restore stream file %s", de->d_name);
cb51ee7a
ZJS
768 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
769 log_warning("Failed to remove /run/systemd/journal/streams/%s: %m",
770 de->d_name);
13790add
LP
771 continue;
772 }
773
774 fdset_remove(fds, fd);
775
776 r = stdout_stream_restore(s, de->d_name, fd);
777 if (r < 0)
778 safe_close(fd);
779 }
a45b9fca 780
7b77ed8c 781 return 0;
13790add
LP
782
783fail:
784 return log_error_errno(errno, "Failed to read streams directory: %m");
a45b9fca
LP
785}
786
15d91bff 787int server_open_stdout_socket(Server *s) {
fc2fffe7
LP
788 static const union sockaddr_union sa = {
789 .un.sun_family = AF_UNIX,
790 .un.sun_path = "/run/systemd/journal/stdout",
791 };
a45b9fca 792 int r;
a45b9fca
LP
793
794 assert(s);
795
796 if (s->stdout_fd < 0) {
a45b9fca 797 s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
4a62c710
MS
798 if (s->stdout_fd < 0)
799 return log_error_errno(errno, "socket() failed: %m");
a45b9fca 800
fc2fffe7 801 (void) unlink(sa.un.sun_path);
a45b9fca 802
fc2fffe7 803 r = bind(s->stdout_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
4a62c710
MS
804 if (r < 0)
805 return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
a45b9fca 806
4a61c3e5 807 (void) chmod(sa.un.sun_path, 0666);
a45b9fca 808
4a62c710
MS
809 if (listen(s->stdout_fd, SOMAXCONN) < 0)
810 return log_error_errno(errno, "listen(%s) failed: %m", sa.un.sun_path);
a45b9fca
LP
811 } else
812 fd_nonblock(s->stdout_fd, 1);
813
151b9b96 814 r = sd_event_add_io(s->event, &s->stdout_event_source, s->stdout_fd, EPOLLIN, stdout_stream_new, s);
23bbb0de
MS
815 if (r < 0)
816 return log_error_errno(r, "Failed to add stdout server fd to event source: %m");
f9a810be 817
48cef295 818 r = sd_event_source_set_priority(s->stdout_event_source, SD_EVENT_PRIORITY_NORMAL+5);
23bbb0de
MS
819 if (r < 0)
820 return log_error_errno(r, "Failed to adjust priority of stdout server event source: %m");
a45b9fca
LP
821
822 return 0;
823}
e22aa3d3
LP
824
825void stdout_stream_send_notify(StdoutStream *s) {
826 struct iovec iovec = {
827 .iov_base = (char*) "FDSTORE=1",
fbd0b64f 828 .iov_len = STRLEN("FDSTORE=1"),
e22aa3d3
LP
829 };
830 struct msghdr msghdr = {
831 .msg_iov = &iovec,
832 .msg_iovlen = 1,
833 };
834 struct cmsghdr *cmsg;
835 ssize_t l;
836
837 assert(s);
838 assert(!s->fdstore);
839 assert(s->in_notify_queue);
840 assert(s->server);
841 assert(s->server->notify_fd >= 0);
842
843 /* Store the connection fd in PID 1, so that we get it passed
844 * in again on next start */
845
846 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
847 msghdr.msg_control = alloca0(msghdr.msg_controllen);
848
849 cmsg = CMSG_FIRSTHDR(&msghdr);
850 cmsg->cmsg_level = SOL_SOCKET;
851 cmsg->cmsg_type = SCM_RIGHTS;
852 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
853
854 memcpy(CMSG_DATA(cmsg), &s->fd, sizeof(int));
855
856 l = sendmsg(s->server->notify_fd, &msghdr, MSG_DONTWAIT|MSG_NOSIGNAL);
857 if (l < 0) {
858 if (errno == EAGAIN)
859 return;
860
861 log_error_errno(errno, "Failed to send stream file descriptor to service manager: %m");
862 } else {
863 log_debug("Successfully sent stream file descriptor to service manager.");
864 s->fdstore = 1;
865 }
866
867 LIST_REMOVE(stdout_stream_notify_queue, s->server->stdout_streams_notify_queue, s);
868 s->in_notify_queue = false;
869
870}