1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <selinux/selinux.h>
10 #include "sd-daemon.h"
13 #include "alloc-util.h"
14 #include "dirent-util.h"
16 #include "errno-util.h"
22 #include "journald-console.h"
23 #include "journald-context.h"
24 #include "journald-kmsg.h"
25 #include "journald-server.h"
26 #include "journald-stream.h"
27 #include "journald-syslog.h"
28 #include "journald-wall.h"
30 #include "parse-util.h"
31 #include "process-util.h"
32 #include "selinux-util.h"
33 #include "socket-util.h"
34 #include "stdio-util.h"
35 #include "string-util.h"
36 #include "syslog-util.h"
37 #include "tmpfile-util.h"
38 #include "unit-name.h"
39 #include "user-util.h"
41 #define STDOUT_STREAMS_MAX 4096
43 /* During the "setup" protocol phase of the stream logic let's define a different maximum line length than
44 * during the actual operational phase. We want to allow users to specify very short line lengths after all,
45 * but the unit name we embed in the setup protocol might be longer than that. Hence, during the setup phase
46 * let's enforce a line length matching the maximum unit name length (255) */
47 #define STDOUT_STREAM_SETUP_PROTOCOL_LINE_MAX (UNIT_NAME_MAX-1U)
49 typedef enum StdoutStreamState
{
50 STDOUT_STREAM_IDENTIFIER
,
51 STDOUT_STREAM_UNIT_ID
,
52 STDOUT_STREAM_PRIORITY
,
53 STDOUT_STREAM_LEVEL_PREFIX
,
54 STDOUT_STREAM_FORWARD_TO_SYSLOG
,
55 STDOUT_STREAM_FORWARD_TO_KMSG
,
56 STDOUT_STREAM_FORWARD_TO_CONSOLE
,
57 STDOUT_STREAM_RUNNING
,
60 /* The different types of log record terminators: a real \n was read, a NUL character was read, the maximum line length
61 * was reached, or the end of the stream was reached */
63 typedef enum LineBreak
{
68 LINE_BREAK_PID_CHANGE
,
70 _LINE_BREAK_INVALID
= -EINVAL
,
75 StdoutStreamState state
;
85 bool forward_to_syslog
:1;
86 bool forward_to_kmsg
:1;
87 bool forward_to_console
:1;
90 bool in_notify_queue
:1;
95 sd_event_source
*event_source
;
99 ClientContext
*context
;
101 LIST_FIELDS(StdoutStream
, stdout_stream
);
102 LIST_FIELDS(StdoutStream
, stdout_stream_notify_queue
);
104 char id_field
[STRLEN("_STREAM_ID=") + SD_ID128_STRING_MAX
];
107 StdoutStream
* stdout_stream_free(StdoutStream
*s
) {
113 client_context_release(s
->server
, s
->context
);
115 assert(s
->server
->n_stdout_streams
> 0);
116 s
->server
->n_stdout_streams
--;
117 LIST_REMOVE(stdout_stream
, s
->server
->stdout_streams
, s
);
119 if (s
->in_notify_queue
)
120 LIST_REMOVE(stdout_stream_notify_queue
, s
->server
->stdout_streams_notify_queue
, s
);
122 (void) server_start_or_stop_idle_timer(s
->server
); /* Maybe we are idle now? */
125 sd_event_source_disable_unref(s
->event_source
);
136 DEFINE_TRIVIAL_CLEANUP_FUNC(StdoutStream
*, stdout_stream_free
);
138 void stdout_stream_destroy(StdoutStream
*s
) {
143 (void) unlink(s
->state_file
);
145 stdout_stream_free(s
);
148 static int stdout_stream_save(StdoutStream
*s
) {
149 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
150 _cleanup_fclose_
FILE *f
= NULL
;
155 if (s
->state
!= STDOUT_STREAM_RUNNING
)
158 if (!s
->state_file
) {
161 r
= fstat(s
->fd
, &st
);
163 return log_warning_errno(errno
, "Failed to stat connected stream: %m");
165 /* We use device and inode numbers as identifier for the stream */
166 r
= asprintf(&s
->state_file
, "%s/streams/%lu:%lu", s
->server
->runtime_directory
, (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
171 (void) mkdir_parents(s
->state_file
, 0755);
173 r
= fopen_temporary(s
->state_file
, &f
, &temp_path
);
178 "# This is private data. Do not parse\n"
181 "FORWARD_TO_SYSLOG=%i\n"
182 "FORWARD_TO_KMSG=%i\n"
183 "FORWARD_TO_CONSOLE=%i\n"
187 s
->forward_to_syslog
,
189 s
->forward_to_console
,
190 s
->id_field
+ STRLEN("_STREAM_ID="));
192 if (!isempty(s
->identifier
)) {
193 _cleanup_free_
char *escaped
= NULL
;
195 escaped
= cescape(s
->identifier
);
201 fprintf(f
, "IDENTIFIER=%s\n", escaped
);
204 if (!isempty(s
->unit_id
)) {
205 _cleanup_free_
char *escaped
= NULL
;
207 escaped
= cescape(s
->unit_id
);
213 fprintf(f
, "UNIT=%s\n", escaped
);
216 r
= fflush_and_check(f
);
220 if (rename(temp_path
, s
->state_file
) < 0) {
225 temp_path
= mfree(temp_path
);
227 if (!s
->fdstore
&& !s
->in_notify_queue
) {
228 LIST_PREPEND(stdout_stream_notify_queue
, s
->server
->stdout_streams_notify_queue
, s
);
229 s
->in_notify_queue
= true;
231 if (s
->server
->notify_event_source
) {
232 r
= sd_event_source_set_enabled(s
->server
->notify_event_source
, SD_EVENT_ON
);
234 log_warning_errno(r
, "Failed to enable notify event source: %m");
241 (void) unlink(s
->state_file
);
242 return log_error_errno(r
, "Failed to save stream data %s: %m", s
->state_file
);
245 static int stdout_stream_log(
248 LineBreak line_break
) {
252 char syslog_priority
[] = "PRIORITY=\0";
253 char syslog_facility
[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int) + 1];
254 _cleanup_free_
char *message
= NULL
, *syslog_identifier
= NULL
;
261 assert(line_break
>= 0);
262 assert(line_break
< _LINE_BREAK_MAX
);
265 (void) client_context_maybe_refresh(s
->server
, s
->context
, NULL
, NULL
, 0, NULL
, USEC_INFINITY
);
266 else if (pid_is_valid(s
->ucred
.pid
)) {
267 r
= client_context_acquire(s
->server
, s
->ucred
.pid
, &s
->ucred
, s
->label
, strlen_ptr(s
->label
), s
->unit_id
, &s
->context
);
269 log_warning_errno(r
, "Failed to acquire client context, ignoring: %m");
272 priority
= s
->priority
;
275 syslog_parse_priority(&p
, &priority
, false);
277 if (!client_context_test_priority(s
->context
, priority
))
283 if (s
->forward_to_syslog
|| s
->server
->forward_to_syslog
)
284 server_forward_syslog(s
->server
, syslog_fixup_facility(priority
), s
->identifier
, p
, &s
->ucred
, NULL
);
286 if (s
->forward_to_kmsg
|| s
->server
->forward_to_kmsg
)
287 server_forward_kmsg(s
->server
, priority
, s
->identifier
, p
, &s
->ucred
);
289 if (s
->forward_to_console
|| s
->server
->forward_to_console
)
290 server_forward_console(s
->server
, priority
, s
->identifier
, p
, &s
->ucred
);
292 if (s
->server
->forward_to_wall
)
293 server_forward_wall(s
->server
, priority
, s
->identifier
, p
, &s
->ucred
);
295 m
= N_IOVEC_META_FIELDS
+ 7 + client_context_extra_fields_n_iovec(s
->context
);
296 iovec
= newa(struct iovec
, m
);
298 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=stdout");
299 iovec
[n
++] = IOVEC_MAKE_STRING(s
->id_field
);
301 syslog_priority
[STRLEN("PRIORITY=")] = '0' + LOG_PRI(priority
);
302 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
304 if (priority
& LOG_FACMASK
) {
305 xsprintf(syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
));
306 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
310 syslog_identifier
= strjoin("SYSLOG_IDENTIFIER=", s
->identifier
);
311 if (syslog_identifier
)
312 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_identifier
);
315 static const char * const line_break_field_table
[_LINE_BREAK_MAX
] = {
316 [LINE_BREAK_NEWLINE
] = NULL
, /* Do not add field if traditional newline */
317 [LINE_BREAK_NUL
] = "_LINE_BREAK=nul",
318 [LINE_BREAK_LINE_MAX
] = "_LINE_BREAK=line-max",
319 [LINE_BREAK_EOF
] = "_LINE_BREAK=eof",
320 [LINE_BREAK_PID_CHANGE
] = "_LINE_BREAK=pid-change",
323 const char *c
= line_break_field_table
[line_break
];
325 /* If this log message was generated due to an uncommon line break then mention this in the log
328 iovec
[n
++] = IOVEC_MAKE_STRING(c
);
330 message
= strjoin("MESSAGE=", p
);
332 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
334 server_dispatch_message(s
->server
, iovec
, n
, m
, s
->context
, NULL
, priority
, 0);
338 static int syslog_parse_priority_and_facility(const char *s
) {
341 /* Parses both facility and priority in one value, i.e. is different from log_level_from_string()
342 * which only parses the priority and refuses any facility value */
344 r
= safe_atoi(s
, &prio
);
348 if (prio
< 0 || prio
> 999)
354 static int stdout_stream_line(StdoutStream
*s
, char *p
, LineBreak line_break
) {
364 /* line breaks by NUL, line max length or EOF are not permissible during the negotiation part of the protocol */
365 if (line_break
!= LINE_BREAK_NEWLINE
&& s
->state
!= STDOUT_STREAM_RUNNING
)
366 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
367 "Control protocol line not properly terminated.");
371 case STDOUT_STREAM_IDENTIFIER
:
373 s
->identifier
= strdup(p
);
378 s
->state
= STDOUT_STREAM_UNIT_ID
;
381 case STDOUT_STREAM_UNIT_ID
:
382 if (s
->ucred
.uid
== 0 &&
383 unit_name_is_valid(p
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
)) {
385 s
->unit_id
= strdup(p
);
390 s
->state
= STDOUT_STREAM_PRIORITY
;
393 case STDOUT_STREAM_PRIORITY
: {
396 priority
= syslog_parse_priority_and_facility(p
);
398 return log_warning_errno(priority
, "Failed to parse log priority line: %m");
400 s
->priority
= priority
;
401 s
->state
= STDOUT_STREAM_LEVEL_PREFIX
;
405 case STDOUT_STREAM_LEVEL_PREFIX
:
406 r
= parse_boolean(p
);
408 return log_warning_errno(r
, "Failed to parse level prefix line: %m");
411 s
->state
= STDOUT_STREAM_FORWARD_TO_SYSLOG
;
414 case STDOUT_STREAM_FORWARD_TO_SYSLOG
:
415 r
= parse_boolean(p
);
417 return log_warning_errno(r
, "Failed to parse forward to syslog line: %m");
419 s
->forward_to_syslog
= r
;
420 s
->state
= STDOUT_STREAM_FORWARD_TO_KMSG
;
423 case STDOUT_STREAM_FORWARD_TO_KMSG
:
424 r
= parse_boolean(p
);
426 return log_warning_errno(r
, "Failed to parse copy to kmsg line: %m");
428 s
->forward_to_kmsg
= r
;
429 s
->state
= STDOUT_STREAM_FORWARD_TO_CONSOLE
;
432 case STDOUT_STREAM_FORWARD_TO_CONSOLE
:
433 r
= parse_boolean(p
);
435 return log_warning_errno(r
, "Failed to parse copy to console line.");
437 s
->forward_to_console
= r
;
438 s
->state
= STDOUT_STREAM_RUNNING
;
440 /* Try to save the stream, so that journald can be restarted and we can recover */
441 (void) stdout_stream_save(s
);
444 case STDOUT_STREAM_RUNNING
:
445 return stdout_stream_log(s
, orig
, line_break
);
448 assert_not_reached();
451 static int stdout_stream_found(
455 LineBreak line_break
) {
463 /* Let's NUL terminate the specified buffer for this call, and revert back afterwards */
466 r
= stdout_stream_line(s
, p
, line_break
);
472 static size_t stdout_stream_line_max(StdoutStream
*s
) {
475 /* During the "setup" phase of our protocol, let's ensure we use a line length where a full unit name
477 if (s
->state
!= STDOUT_STREAM_RUNNING
)
478 return STDOUT_STREAM_SETUP_PROTOCOL_LINE_MAX
;
480 /* After the protocol's "setup" phase is complete, let's use whatever the user configured */
481 return s
->server
->line_max
;
484 static int stdout_stream_scan(
488 LineBreak force_flush
,
489 size_t *ret_consumed
) {
491 size_t consumed
= 0, line_max
;
497 line_max
= stdout_stream_line_max(s
);
500 LineBreak line_break
;
503 size_t tmp_remaining
= MIN(remaining
, line_max
);
505 end1
= memchr(p
, '\n', tmp_remaining
);
506 end2
= memchr(p
, 0, end1
? (size_t) (end1
- p
) : tmp_remaining
);
509 /* We found a NUL terminator */
512 line_break
= LINE_BREAK_NUL
;
514 /* We found a \n terminator */
517 line_break
= LINE_BREAK_NEWLINE
;
518 } else if (remaining
>= line_max
) {
519 /* Force a line break after the maximum line length */
520 found
= skip
= line_max
;
521 line_break
= LINE_BREAK_LINE_MAX
;
525 r
= stdout_stream_found(s
, p
, found
, line_break
);
534 if (force_flush
>= 0 && remaining
> 0) {
535 r
= stdout_stream_found(s
, p
, remaining
, force_flush
);
539 consumed
+= remaining
;
543 *ret_consumed
= consumed
;
548 static int stdout_stream_process(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
549 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred
))) control
;
550 size_t limit
, consumed
, allocated
;
551 StdoutStream
*s
= ASSERT_PTR(userdata
);
558 struct msghdr msghdr
= {
561 .msg_control
= &control
,
562 .msg_controllen
= sizeof(control
),
565 if ((revents
|EPOLLIN
|EPOLLHUP
) != (EPOLLIN
|EPOLLHUP
)) {
566 log_error("Got invalid event from epoll for stdout stream: %"PRIx32
, revents
);
570 /* If the buffer is almost full, add room for another 1K */
571 allocated
= MALLOC_ELEMENTSOF(s
->buffer
);
572 if (s
->length
+ 512 >= allocated
) {
573 if (!GREEDY_REALLOC(s
->buffer
, s
->length
+ 1 + 1024)) {
578 allocated
= MALLOC_ELEMENTSOF(s
->buffer
);
581 /* Try to make use of the allocated buffer in full, but never read more than the configured line size. Also,
582 * always leave room for a terminating NUL we might need to add. */
583 limit
= MIN(allocated
- 1, MAX(s
->server
->line_max
, STDOUT_STREAM_SETUP_PROTOCOL_LINE_MAX
));
584 assert(s
->length
<= limit
);
585 iovec
= IOVEC_MAKE(s
->buffer
+ s
->length
, limit
- s
->length
);
587 l
= recvmsg(s
->fd
, &msghdr
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
589 if (ERRNO_IS_TRANSIENT(errno
))
592 log_warning_errno(errno
, "Failed to read from stream: %m");
595 cmsg_close_all(&msghdr
);
598 (void) stdout_stream_scan(s
, s
->buffer
, s
->length
, /* force_flush = */ LINE_BREAK_EOF
, NULL
);
602 /* Invalidate the context if the PID of the sender changed. This happens when a forked process
603 * inherits stdout/stderr from a parent. In this case getpeercred() returns the ucred of the parent,
604 * which can be invalid if the parent has exited in the meantime. */
605 ucred
= CMSG_FIND_DATA(&msghdr
, SOL_SOCKET
, SCM_CREDENTIALS
, struct ucred
);
606 if (ucred
&& ucred
->pid
!= s
->ucred
.pid
) {
607 /* Force out any previously half-written lines from a different process, before we switch to
608 * the new ucred structure for everything we just added */
609 r
= stdout_stream_scan(s
, s
->buffer
, s
->length
, /* force_flush = */ LINE_BREAK_PID_CHANGE
, NULL
);
613 s
->context
= client_context_release(s
->server
, s
->context
);
615 p
= s
->buffer
+ s
->length
;
621 /* Always copy in the new credentials */
625 r
= stdout_stream_scan(s
, p
, l
, _LINE_BREAK_INVALID
, &consumed
);
629 /* Move what wasn't consumed to the front of the buffer */
630 assert(consumed
<= (size_t) l
);
631 s
->length
= l
- consumed
;
632 memmove(s
->buffer
, p
+ consumed
, s
->length
);
637 stdout_stream_destroy(s
);
641 int stdout_stream_install(Server
*s
, int fd
, StdoutStream
**ret
) {
642 _cleanup_(stdout_stream_freep
) StdoutStream
*stream
= NULL
;
649 r
= sd_id128_randomize(&id
);
651 return log_error_errno(r
, "Failed to generate stream ID: %m");
653 stream
= new(StdoutStream
, 1);
657 *stream
= (StdoutStream
) {
659 .priority
= LOG_INFO
,
660 .ucred
= UCRED_INVALID
,
663 xsprintf(stream
->id_field
, "_STREAM_ID=" SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(id
));
665 r
= getpeercred(fd
, &stream
->ucred
);
667 return log_error_errno(r
, "Failed to determine peer credentials: %m");
669 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSCRED
, true);
671 return log_error_errno(r
, "SO_PASSCRED failed: %m");
673 if (mac_selinux_use()) {
674 r
= getpeersec(fd
, &stream
->label
);
675 if (r
< 0 && r
!= -EOPNOTSUPP
)
676 (void) log_warning_errno(r
, "Failed to determine peer security context: %m");
679 (void) shutdown(fd
, SHUT_WR
);
681 r
= sd_event_add_io(s
->event
, &stream
->event_source
, fd
, EPOLLIN
, stdout_stream_process
, stream
);
683 return log_error_errno(r
, "Failed to add stream to event loop: %m");
685 r
= sd_event_source_set_priority(stream
->event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
687 return log_error_errno(r
, "Failed to adjust stdout event source priority: %m");
692 LIST_PREPEND(stdout_stream
, s
->stdout_streams
, stream
);
693 s
->n_stdout_streams
++;
695 (void) server_start_or_stop_idle_timer(s
); /* Maybe no longer idle? */
704 static int stdout_stream_new(sd_event_source
*es
, int listen_fd
, uint32_t revents
, void *userdata
) {
705 _cleanup_close_
int fd
= -1;
706 Server
*s
= ASSERT_PTR(userdata
);
709 if (revents
!= EPOLLIN
)
710 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
711 "Got invalid event from epoll for stdout server fd: %" PRIx32
,
714 fd
= accept4(s
->stdout_fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
716 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
719 return log_error_errno(errno
, "Failed to accept stdout connection: %m");
722 if (s
->n_stdout_streams
>= STDOUT_STREAMS_MAX
) {
723 struct ucred u
= UCRED_INVALID
;
725 (void) getpeercred(fd
, &u
);
727 /* By closing fd here we make sure that the client won't wait too long for journald to
728 * gather all the data it adds to the error message to find out that the connection has
733 server_driver_message(s
, u
.pid
, NULL
, LOG_MESSAGE("Too many stdout streams, refusing connection."), NULL
);
737 r
= stdout_stream_install(s
, fd
, NULL
);
745 static int stdout_stream_load(StdoutStream
*stream
, const char *fname
) {
748 *level_prefix
= NULL
,
749 *forward_to_syslog
= NULL
,
750 *forward_to_kmsg
= NULL
,
751 *forward_to_console
= NULL
,
758 if (!stream
->state_file
) {
759 stream
->state_file
= path_join(stream
->server
->runtime_directory
, "streams", fname
);
760 if (!stream
->state_file
)
764 r
= parse_env_file(NULL
, stream
->state_file
,
765 "PRIORITY", &priority
,
766 "LEVEL_PREFIX", &level_prefix
,
767 "FORWARD_TO_SYSLOG", &forward_to_syslog
,
768 "FORWARD_TO_KMSG", &forward_to_kmsg
,
769 "FORWARD_TO_CONSOLE", &forward_to_console
,
770 "IDENTIFIER", &stream
->identifier
,
771 "UNIT", &stream
->unit_id
,
772 "STREAM_ID", &stream_id
);
774 return log_error_errno(r
, "Failed to read: %s", stream
->state_file
);
779 p
= syslog_parse_priority_and_facility(priority
);
781 stream
->priority
= p
;
785 r
= parse_boolean(level_prefix
);
787 stream
->level_prefix
= r
;
790 if (forward_to_syslog
) {
791 r
= parse_boolean(forward_to_syslog
);
793 stream
->forward_to_syslog
= r
;
796 if (forward_to_kmsg
) {
797 r
= parse_boolean(forward_to_kmsg
);
799 stream
->forward_to_kmsg
= r
;
802 if (forward_to_console
) {
803 r
= parse_boolean(forward_to_console
);
805 stream
->forward_to_console
= r
;
811 r
= sd_id128_from_string(stream_id
, &id
);
813 xsprintf(stream
->id_field
, "_STREAM_ID=" SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(id
));
819 static int stdout_stream_restore(Server
*s
, const char *fname
, int fd
) {
820 StdoutStream
*stream
;
827 if (s
->n_stdout_streams
>= STDOUT_STREAMS_MAX
) {
828 log_warning("Too many stdout streams, refusing restoring of stream.");
832 r
= stdout_stream_install(s
, fd
, &stream
);
836 stream
->state
= STDOUT_STREAM_RUNNING
;
837 stream
->fdstore
= true;
839 /* Ignore all parsing errors */
840 (void) stdout_stream_load(stream
, fname
);
845 int server_restore_streams(Server
*s
, FDSet
*fds
) {
846 _cleanup_closedir_
DIR *d
= NULL
;
850 path
= strjoina(s
->runtime_directory
, "/streams");
856 return log_warning_errno(errno
, "Failed to enumerate %s: %m", path
);
859 FOREACH_DIRENT(de
, d
, goto fail
) {
860 unsigned long st_dev
, st_ino
;
864 if (sscanf(de
->d_name
, "%lu:%lu", &st_dev
, &st_ino
) != 2)
867 FDSET_FOREACH(fd
, fds
) {
870 if (fstat(fd
, &st
) < 0)
871 return log_error_errno(errno
, "Failed to stat %s: %m", de
->d_name
);
873 if (S_ISSOCK(st
.st_mode
) && st
.st_dev
== st_dev
&& st
.st_ino
== st_ino
) {
880 /* No file descriptor? Then let's delete the state file */
881 log_debug("Cannot restore stream file %s", de
->d_name
);
882 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
883 log_warning_errno(errno
, "Failed to remove %s/%s: %m", path
, de
->d_name
);
887 fdset_remove(fds
, fd
);
889 r
= stdout_stream_restore(s
, de
->d_name
, fd
);
897 return log_error_errno(errno
, "Failed to read streams directory: %m");
900 int server_open_stdout_socket(Server
*s
, const char *stdout_socket
) {
904 assert(stdout_socket
);
906 if (s
->stdout_fd
< 0) {
907 union sockaddr_union sa
;
910 r
= sockaddr_un_set_path(&sa
.un
, stdout_socket
);
912 return log_error_errno(r
, "Unable to use namespace path %s for AF_UNIX socket: %m", stdout_socket
);
915 s
->stdout_fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
916 if (s
->stdout_fd
< 0)
917 return log_error_errno(errno
, "socket() failed: %m");
919 (void) sockaddr_un_unlink(&sa
.un
);
921 r
= bind(s
->stdout_fd
, &sa
.sa
, sa_len
);
923 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
925 (void) chmod(sa
.un
.sun_path
, 0666);
927 if (listen(s
->stdout_fd
, SOMAXCONN
) < 0)
928 return log_error_errno(errno
, "listen(%s) failed: %m", sa
.un
.sun_path
);
930 (void) fd_nonblock(s
->stdout_fd
, true);
932 r
= sd_event_add_io(s
->event
, &s
->stdout_event_source
, s
->stdout_fd
, EPOLLIN
, stdout_stream_new
, s
);
934 return log_error_errno(r
, "Failed to add stdout server fd to event source: %m");
936 r
= sd_event_source_set_priority(s
->stdout_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
938 return log_error_errno(r
, "Failed to adjust priority of stdout server event source: %m");
943 void stdout_stream_send_notify(StdoutStream
*s
) {
944 struct iovec iovec
= {
945 .iov_base
= (char*) "FDSTORE=1",
946 .iov_len
= STRLEN("FDSTORE=1"),
948 struct msghdr msghdr
= {
952 struct cmsghdr
*cmsg
;
957 assert(s
->in_notify_queue
);
959 assert(s
->server
->notify_fd
>= 0);
961 /* Store the connection fd in PID 1, so that we get it passed
962 * in again on next start */
964 msghdr
.msg_controllen
= CMSG_SPACE(sizeof(int));
965 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
967 cmsg
= CMSG_FIRSTHDR(&msghdr
);
968 cmsg
->cmsg_level
= SOL_SOCKET
;
969 cmsg
->cmsg_type
= SCM_RIGHTS
;
970 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
972 memcpy(CMSG_DATA(cmsg
), &s
->fd
, sizeof(int));
974 l
= sendmsg(s
->server
->notify_fd
, &msghdr
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
979 log_error_errno(errno
, "Failed to send stream file descriptor to service manager: %m");
981 log_debug("Successfully sent stream file descriptor to service manager.");
985 LIST_REMOVE(stdout_stream_notify_queue
, s
->server
->stdout_streams_notify_queue
, s
);
986 s
->in_notify_queue
= false;