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