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