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