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