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