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