]>
Commit | Line | Data |
---|---|---|
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 | ||
38 | typedef 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 | ||
52 | typedef enum LineBreak { | |
53 | LINE_BREAK_NEWLINE, | |
54 | LINE_BREAK_NUL, | |
55 | LINE_BREAK_LINE_MAX, | |
56 | LINE_BREAK_EOF, | |
57 | } LineBreak; | |
58 | ||
a45b9fca LP |
59 | struct 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 |
94 | void 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 | ||
126 | DEFINE_TRIVIAL_CLEANUP_FUNC(StdoutStream*, stdout_stream_free); | |
127 | ||
128 | static void stdout_stream_destroy(StdoutStream *s) { | |
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 | ||
138 | static 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 |
227 | fail: |
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 | 236 | static 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 | 319 | static 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 | ||
423 | static 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 |
486 | static 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 | 532 | terminate: |
13790add | 533 | stdout_stream_destroy(s); |
f9a810be | 534 | return 0; |
a45b9fca LP |
535 | } |
536 | ||
13790add LP |
537 | static int stdout_stream_install(Server *s, int fd, StdoutStream **ret) { |
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 | 592 | static 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 | ||
f9a810be LP |
599 | if (revents != EPOLLIN) { |
600 | log_error("Got invalid event from epoll for stdout server fd: %"PRIx32, revents); | |
601 | return -EIO; | |
602 | } | |
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) { | |
613 | log_warning("Too many stdout streams, refusing connection."); | |
a45b9fca LP |
614 | return 0; |
615 | } | |
616 | ||
13790add LP |
617 | r = stdout_stream_install(s, fd, NULL); |
618 | if (r < 0) | |
619 | return r; | |
620 | ||
621 | fd = -1; | |
622 | return 0; | |
623 | } | |
624 | ||
625 | static int stdout_stream_load(StdoutStream *stream, const char *fname) { | |
626 | _cleanup_free_ char | |
627 | *priority = NULL, | |
628 | *level_prefix = NULL, | |
629 | *forward_to_syslog = NULL, | |
630 | *forward_to_kmsg = NULL, | |
ec20fe5f LP |
631 | *forward_to_console = NULL, |
632 | *stream_id = NULL; | |
13790add LP |
633 | int r; |
634 | ||
635 | assert(stream); | |
636 | assert(fname); | |
637 | ||
638 | if (!stream->state_file) { | |
639 | stream->state_file = strappend("/run/systemd/journal/streams/", fname); | |
640 | if (!stream->state_file) | |
641 | return log_oom(); | |
a45b9fca LP |
642 | } |
643 | ||
1a5a177e | 644 | r = parse_env_file(NULL, stream->state_file, NEWLINE, |
13790add LP |
645 | "PRIORITY", &priority, |
646 | "LEVEL_PREFIX", &level_prefix, | |
647 | "FORWARD_TO_SYSLOG", &forward_to_syslog, | |
648 | "FORWARD_TO_KMSG", &forward_to_kmsg, | |
649 | "FORWARD_TO_CONSOLE", &forward_to_console, | |
650 | "IDENTIFIER", &stream->identifier, | |
651 | "UNIT", &stream->unit_id, | |
ec20fe5f | 652 | "STREAM_ID", &stream_id, |
13790add LP |
653 | NULL); |
654 | if (r < 0) | |
655 | return log_error_errno(r, "Failed to read: %s", stream->state_file); | |
a45b9fca | 656 | |
13790add LP |
657 | if (priority) { |
658 | int p; | |
659 | ||
660 | p = log_level_from_string(priority); | |
661 | if (p >= 0) | |
662 | stream->priority = p; | |
a45b9fca LP |
663 | } |
664 | ||
13790add LP |
665 | if (level_prefix) { |
666 | r = parse_boolean(level_prefix); | |
667 | if (r >= 0) | |
668 | stream->level_prefix = r; | |
d682b3a7 | 669 | } |
a45b9fca | 670 | |
13790add LP |
671 | if (forward_to_syslog) { |
672 | r = parse_boolean(forward_to_syslog); | |
673 | if (r >= 0) | |
674 | stream->forward_to_syslog = r; | |
a45b9fca LP |
675 | } |
676 | ||
13790add LP |
677 | if (forward_to_kmsg) { |
678 | r = parse_boolean(forward_to_kmsg); | |
679 | if (r >= 0) | |
680 | stream->forward_to_kmsg = r; | |
f9a810be LP |
681 | } |
682 | ||
13790add LP |
683 | if (forward_to_console) { |
684 | r = parse_boolean(forward_to_console); | |
685 | if (r >= 0) | |
686 | stream->forward_to_console = r; | |
a45b9fca LP |
687 | } |
688 | ||
ec20fe5f LP |
689 | if (stream_id) { |
690 | sd_id128_t id; | |
691 | ||
692 | r = sd_id128_from_string(stream_id, &id); | |
693 | if (r >= 0) | |
694 | xsprintf(stream->id_field, "_STREAM_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id)); | |
695 | } | |
696 | ||
13790add LP |
697 | return 0; |
698 | } | |
699 | ||
700 | static int stdout_stream_restore(Server *s, const char *fname, int fd) { | |
701 | StdoutStream *stream; | |
702 | int r; | |
703 | ||
704 | assert(s); | |
705 | assert(fname); | |
706 | assert(fd >= 0); | |
707 | ||
708 | if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) { | |
709 | log_warning("Too many stdout streams, refusing restoring of stream."); | |
710 | return -ENOBUFS; | |
711 | } | |
712 | ||
713 | r = stdout_stream_install(s, fd, &stream); | |
714 | if (r < 0) | |
715 | return r; | |
716 | ||
717 | stream->state = STDOUT_STREAM_RUNNING; | |
718 | stream->fdstore = true; | |
719 | ||
720 | /* Ignore all parsing errors */ | |
721 | (void) stdout_stream_load(stream, fname); | |
a45b9fca LP |
722 | |
723 | return 0; | |
13790add LP |
724 | } |
725 | ||
15d91bff | 726 | int server_restore_streams(Server *s, FDSet *fds) { |
13790add LP |
727 | _cleanup_closedir_ DIR *d = NULL; |
728 | struct dirent *de; | |
729 | int r; | |
730 | ||
731 | d = opendir("/run/systemd/journal/streams"); | |
732 | if (!d) { | |
733 | if (errno == ENOENT) | |
734 | return 0; | |
735 | ||
736 | return log_warning_errno(errno, "Failed to enumerate /run/systemd/journal/streams: %m"); | |
737 | } | |
738 | ||
739 | FOREACH_DIRENT(de, d, goto fail) { | |
740 | unsigned long st_dev, st_ino; | |
741 | bool found = false; | |
742 | Iterator i; | |
743 | int fd; | |
744 | ||
745 | if (sscanf(de->d_name, "%lu:%lu", &st_dev, &st_ino) != 2) | |
746 | continue; | |
747 | ||
748 | FDSET_FOREACH(fd, fds, i) { | |
749 | struct stat st; | |
750 | ||
751 | if (fstat(fd, &st) < 0) | |
752 | return log_error_errno(errno, "Failed to stat %s: %m", de->d_name); | |
753 | ||
754 | if (S_ISSOCK(st.st_mode) && st.st_dev == st_dev && st.st_ino == st_ino) { | |
755 | found = true; | |
756 | break; | |
757 | } | |
758 | } | |
759 | ||
760 | if (!found) { | |
761 | /* No file descriptor? Then let's delete the state file */ | |
762 | log_debug("Cannot restore stream file %s", de->d_name); | |
cb51ee7a | 763 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) |
b8b846d7 LP |
764 | log_warning_errno(errno, "Failed to remove /run/systemd/journal/streams/%s: %m", |
765 | de->d_name); | |
13790add LP |
766 | continue; |
767 | } | |
768 | ||
769 | fdset_remove(fds, fd); | |
770 | ||
771 | r = stdout_stream_restore(s, de->d_name, fd); | |
772 | if (r < 0) | |
773 | safe_close(fd); | |
774 | } | |
a45b9fca | 775 | |
7b77ed8c | 776 | return 0; |
13790add LP |
777 | |
778 | fail: | |
779 | return log_error_errno(errno, "Failed to read streams directory: %m"); | |
a45b9fca LP |
780 | } |
781 | ||
15d91bff | 782 | int server_open_stdout_socket(Server *s) { |
fc2fffe7 LP |
783 | static const union sockaddr_union sa = { |
784 | .un.sun_family = AF_UNIX, | |
785 | .un.sun_path = "/run/systemd/journal/stdout", | |
786 | }; | |
a45b9fca | 787 | int r; |
a45b9fca LP |
788 | |
789 | assert(s); | |
790 | ||
791 | if (s->stdout_fd < 0) { | |
a45b9fca | 792 | s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
4a62c710 MS |
793 | if (s->stdout_fd < 0) |
794 | return log_error_errno(errno, "socket() failed: %m"); | |
a45b9fca | 795 | |
fc2fffe7 | 796 | (void) unlink(sa.un.sun_path); |
a45b9fca | 797 | |
fc2fffe7 | 798 | r = bind(s->stdout_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); |
4a62c710 MS |
799 | if (r < 0) |
800 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); | |
a45b9fca | 801 | |
4a61c3e5 | 802 | (void) chmod(sa.un.sun_path, 0666); |
a45b9fca | 803 | |
4a62c710 MS |
804 | if (listen(s->stdout_fd, SOMAXCONN) < 0) |
805 | return log_error_errno(errno, "listen(%s) failed: %m", sa.un.sun_path); | |
a45b9fca LP |
806 | } else |
807 | fd_nonblock(s->stdout_fd, 1); | |
808 | ||
151b9b96 | 809 | r = sd_event_add_io(s->event, &s->stdout_event_source, s->stdout_fd, EPOLLIN, stdout_stream_new, s); |
23bbb0de MS |
810 | if (r < 0) |
811 | return log_error_errno(r, "Failed to add stdout server fd to event source: %m"); | |
f9a810be | 812 | |
48cef295 | 813 | r = sd_event_source_set_priority(s->stdout_event_source, SD_EVENT_PRIORITY_NORMAL+5); |
23bbb0de MS |
814 | if (r < 0) |
815 | return log_error_errno(r, "Failed to adjust priority of stdout server event source: %m"); | |
a45b9fca LP |
816 | |
817 | return 0; | |
818 | } | |
e22aa3d3 LP |
819 | |
820 | void stdout_stream_send_notify(StdoutStream *s) { | |
821 | struct iovec iovec = { | |
822 | .iov_base = (char*) "FDSTORE=1", | |
fbd0b64f | 823 | .iov_len = STRLEN("FDSTORE=1"), |
e22aa3d3 LP |
824 | }; |
825 | struct msghdr msghdr = { | |
826 | .msg_iov = &iovec, | |
827 | .msg_iovlen = 1, | |
828 | }; | |
829 | struct cmsghdr *cmsg; | |
830 | ssize_t l; | |
831 | ||
832 | assert(s); | |
833 | assert(!s->fdstore); | |
834 | assert(s->in_notify_queue); | |
835 | assert(s->server); | |
836 | assert(s->server->notify_fd >= 0); | |
837 | ||
838 | /* Store the connection fd in PID 1, so that we get it passed | |
839 | * in again on next start */ | |
840 | ||
841 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); | |
842 | msghdr.msg_control = alloca0(msghdr.msg_controllen); | |
843 | ||
844 | cmsg = CMSG_FIRSTHDR(&msghdr); | |
845 | cmsg->cmsg_level = SOL_SOCKET; | |
846 | cmsg->cmsg_type = SCM_RIGHTS; | |
847 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
848 | ||
849 | memcpy(CMSG_DATA(cmsg), &s->fd, sizeof(int)); | |
850 | ||
851 | l = sendmsg(s->server->notify_fd, &msghdr, MSG_DONTWAIT|MSG_NOSIGNAL); | |
852 | if (l < 0) { | |
853 | if (errno == EAGAIN) | |
854 | return; | |
855 | ||
856 | log_error_errno(errno, "Failed to send stream file descriptor to service manager: %m"); | |
857 | } else { | |
858 | log_debug("Successfully sent stream file descriptor to service manager."); | |
859 | s->fdstore = 1; | |
860 | } | |
861 | ||
862 | LIST_REMOVE(stdout_stream_notify_queue, s->server->stdout_streams_notify_queue, s); | |
863 | s->in_notify_queue = false; | |
864 | ||
865 | } |