]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-remote.c
tree-wide: mark set-but-not-used variables as unused to make LLVM happy
[thirdparty/systemd.git] / src / journal-remote / journal-remote.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <sys/prctl.h>
7 #include <stdint.h>
8
9 #include "sd-daemon.h"
10
11 #include "af-list.h"
12 #include "alloc-util.h"
13 #include "def.h"
14 #include "errno-util.h"
15 #include "escape.h"
16 #include "fd-util.h"
17 #include "journal-file.h"
18 #include "journal-remote-write.h"
19 #include "journal-remote.h"
20 #include "journald-native.h"
21 #include "macro.h"
22 #include "parse-util.h"
23 #include "process-util.h"
24 #include "socket-util.h"
25 #include "stdio-util.h"
26 #include "string-util.h"
27 #include "strv.h"
28
29 #define REMOTE_JOURNAL_PATH "/var/log/journal/remote"
30
31 #define filename_escape(s) xescape((s), "/ ")
32
33 static int open_output(RemoteServer *s, Writer *w, const char* host) {
34 _cleanup_free_ char *_filename = NULL;
35 const char *filename;
36 int r;
37
38 switch (s->split_mode) {
39 case JOURNAL_WRITE_SPLIT_NONE:
40 filename = s->output;
41 break;
42
43 case JOURNAL_WRITE_SPLIT_HOST: {
44 _cleanup_free_ char *name = NULL;
45
46 assert(host);
47
48 name = filename_escape(host);
49 if (!name)
50 return log_oom();
51
52 r = asprintf(&_filename, "%s/remote-%s.journal", s->output, name);
53 if (r < 0)
54 return log_oom();
55
56 filename = _filename;
57 break;
58 }
59
60 default:
61 assert_not_reached();
62 }
63
64 r = journal_file_open_reliably(filename,
65 O_RDWR|O_CREAT, 0640,
66 s->compress, UINT64_MAX, s->seal,
67 &w->metrics,
68 w->mmap, NULL,
69 NULL, &w->journal);
70 if (r < 0)
71 return log_error_errno(r, "Failed to open output journal %s: %m", filename);
72
73 log_debug("Opened output file %s", w->journal->path);
74 return 0;
75 }
76
77 /**********************************************************************
78 **********************************************************************
79 **********************************************************************/
80
81 static int init_writer_hashmap(RemoteServer *s) {
82 static const struct hash_ops* const hash_ops[] = {
83 [JOURNAL_WRITE_SPLIT_NONE] = NULL,
84 [JOURNAL_WRITE_SPLIT_HOST] = &string_hash_ops,
85 };
86
87 assert(s);
88 assert(s->split_mode >= 0 && s->split_mode < (int) ELEMENTSOF(hash_ops));
89
90 s->writers = hashmap_new(hash_ops[s->split_mode]);
91 if (!s->writers)
92 return log_oom();
93
94 return 0;
95 }
96
97 int journal_remote_get_writer(RemoteServer *s, const char *host, Writer **writer) {
98 _cleanup_(writer_unrefp) Writer *w = NULL;
99 const void *key;
100 int r;
101
102 switch(s->split_mode) {
103 case JOURNAL_WRITE_SPLIT_NONE:
104 key = "one and only";
105 break;
106
107 case JOURNAL_WRITE_SPLIT_HOST:
108 assert(host);
109 key = host;
110 break;
111
112 default:
113 assert_not_reached();
114 }
115
116 w = hashmap_get(s->writers, key);
117 if (w)
118 writer_ref(w);
119 else {
120 w = writer_new(s);
121 if (!w)
122 return log_oom();
123
124 if (s->split_mode == JOURNAL_WRITE_SPLIT_HOST) {
125 w->hashmap_key = strdup(key);
126 if (!w->hashmap_key)
127 return log_oom();
128 }
129
130 r = open_output(s, w, host);
131 if (r < 0)
132 return r;
133
134 r = hashmap_put(s->writers, w->hashmap_key ?: key, w);
135 if (r < 0)
136 return r;
137 }
138
139 *writer = TAKE_PTR(w);
140
141 return 0;
142 }
143
144 /**********************************************************************
145 **********************************************************************
146 **********************************************************************/
147
148 /* This should go away as soon as µhttpd allows state to be passed around. */
149 RemoteServer *journal_remote_server_global;
150
151 static int dispatch_raw_source_event(sd_event_source *event,
152 int fd,
153 uint32_t revents,
154 void *userdata);
155 static int dispatch_raw_source_until_block(sd_event_source *event,
156 void *userdata);
157 static int dispatch_blocking_source_event(sd_event_source *event,
158 void *userdata);
159 static int dispatch_raw_connection_event(sd_event_source *event,
160 int fd,
161 uint32_t revents,
162 void *userdata);
163
164 static int get_source_for_fd(RemoteServer *s,
165 int fd, char *name, RemoteSource **source) {
166 Writer *writer;
167 int r;
168
169 /* This takes ownership of name, but only on success. */
170
171 assert(fd >= 0);
172 assert(source);
173
174 if (!GREEDY_REALLOC0(s->sources, fd + 1))
175 return log_oom();
176
177 r = journal_remote_get_writer(s, name, &writer);
178 if (r < 0)
179 return log_warning_errno(r, "Failed to get writer for source %s: %m",
180 name);
181
182 if (!s->sources[fd]) {
183 s->sources[fd] = source_new(fd, false, name, writer);
184 if (!s->sources[fd]) {
185 writer_unref(writer);
186 return log_oom();
187 }
188
189 s->active++;
190 }
191
192 *source = s->sources[fd];
193 return 0;
194 }
195
196 static int remove_source(RemoteServer *s, int fd) {
197 RemoteSource *source;
198
199 assert(s);
200 assert(fd >= 0 && fd < (ssize_t) MALLOC_ELEMENTSOF(s->sources));
201
202 source = s->sources[fd];
203 if (source) {
204 /* this closes fd too */
205 source_free(source);
206 s->sources[fd] = NULL;
207 s->active--;
208 }
209
210 return 0;
211 }
212
213 int journal_remote_add_source(RemoteServer *s, int fd, char* name, bool own_name) {
214 RemoteSource *source = NULL;
215 int r;
216
217 /* This takes ownership of name, even on failure, if own_name is true. */
218
219 assert(s);
220 assert(fd >= 0);
221 assert(name);
222
223 if (!own_name) {
224 name = strdup(name);
225 if (!name)
226 return log_oom();
227 }
228
229 r = get_source_for_fd(s, fd, name, &source);
230 if (r < 0) {
231 log_error_errno(r, "Failed to create source for fd:%d (%s): %m",
232 fd, name);
233 free(name);
234 return r;
235 }
236
237 r = sd_event_add_io(s->events, &source->event,
238 fd, EPOLLIN|EPOLLRDHUP|EPOLLPRI,
239 dispatch_raw_source_event, source);
240 if (r == 0) {
241 /* Add additional source for buffer processing. It will be
242 * enabled later. */
243 r = sd_event_add_defer(s->events, &source->buffer_event,
244 dispatch_raw_source_until_block, source);
245 if (r == 0)
246 sd_event_source_set_enabled(source->buffer_event, SD_EVENT_OFF);
247 } else if (r == -EPERM) {
248 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd, name);
249 r = sd_event_add_defer(s->events, &source->event,
250 dispatch_blocking_source_event, source);
251 if (r == 0)
252 sd_event_source_set_enabled(source->event, SD_EVENT_ON);
253 }
254 if (r < 0) {
255 log_error_errno(r, "Failed to register event source for fd:%d: %m",
256 fd);
257 goto error;
258 }
259
260 r = sd_event_source_set_description(source->event, name);
261 if (r < 0) {
262 log_error_errno(r, "Failed to set source name for fd:%d: %m", fd);
263 goto error;
264 }
265
266 return 1; /* work to do */
267
268 error:
269 remove_source(s, fd);
270 return r;
271 }
272
273 int journal_remote_add_raw_socket(RemoteServer *s, int fd) {
274 int r;
275 _unused_ _cleanup_close_ int fd_ = fd;
276 char name[STRLEN("raw-socket-") + DECIMAL_STR_MAX(int) + 1];
277
278 assert(fd >= 0);
279
280 r = sd_event_add_io(s->events, &s->listen_event,
281 fd, EPOLLIN,
282 dispatch_raw_connection_event, s);
283 if (r < 0)
284 return r;
285
286 xsprintf(name, "raw-socket-%d", fd);
287
288 r = sd_event_source_set_description(s->listen_event, name);
289 if (r < 0)
290 return r;
291
292 fd_ = -1;
293 s->active++;
294 return 0;
295 }
296
297 /**********************************************************************
298 **********************************************************************
299 **********************************************************************/
300
301 int journal_remote_server_init(
302 RemoteServer *s,
303 const char *output,
304 JournalWriteSplitMode split_mode,
305 bool compress,
306 bool seal) {
307
308 int r;
309
310 assert(s);
311
312 assert(journal_remote_server_global == NULL);
313 journal_remote_server_global = s;
314
315 s->split_mode = split_mode;
316 s->compress = compress;
317 s->seal = seal;
318
319 if (output)
320 s->output = output;
321 else if (split_mode == JOURNAL_WRITE_SPLIT_NONE)
322 s->output = REMOTE_JOURNAL_PATH "/remote.journal";
323 else if (split_mode == JOURNAL_WRITE_SPLIT_HOST)
324 s->output = REMOTE_JOURNAL_PATH;
325 else
326 assert_not_reached();
327
328 r = sd_event_default(&s->events);
329 if (r < 0)
330 return log_error_errno(r, "Failed to allocate event loop: %m");
331
332 r = init_writer_hashmap(s);
333 if (r < 0)
334 return r;
335
336 return 0;
337 }
338
339 #if HAVE_MICROHTTPD
340 static void MHDDaemonWrapper_free(MHDDaemonWrapper *d) {
341 MHD_stop_daemon(d->daemon);
342 sd_event_source_unref(d->io_event);
343 sd_event_source_unref(d->timer_event);
344 free(d);
345 }
346 #endif
347
348 void journal_remote_server_destroy(RemoteServer *s) {
349 size_t i;
350
351 #if HAVE_MICROHTTPD
352 hashmap_free_with_destructor(s->daemons, MHDDaemonWrapper_free);
353 #endif
354
355 for (i = 0; i < MALLOC_ELEMENTSOF(s->sources); i++)
356 remove_source(s, i);
357 free(s->sources);
358
359 writer_unref(s->_single_writer);
360 hashmap_free(s->writers);
361
362 sd_event_source_unref(s->sigterm_event);
363 sd_event_source_unref(s->sigint_event);
364 sd_event_source_unref(s->listen_event);
365 sd_event_unref(s->events);
366
367 if (s == journal_remote_server_global)
368 journal_remote_server_global = NULL;
369
370 /* fds that we're listening on remain open... */
371 }
372
373 /**********************************************************************
374 **********************************************************************
375 **********************************************************************/
376
377 int journal_remote_handle_raw_source(
378 sd_event_source *event,
379 int fd,
380 uint32_t revents,
381 RemoteServer *s) {
382
383 RemoteSource *source;
384 int r;
385
386 /* Returns 1 if there might be more data pending,
387 * 0 if data is currently exhausted, negative on error.
388 */
389
390 assert(fd >= 0 && fd < (ssize_t) MALLOC_ELEMENTSOF(s->sources));
391 source = s->sources[fd];
392 assert(source->importer.fd == fd);
393
394 r = process_source(source, s->compress, s->seal);
395 if (journal_importer_eof(&source->importer)) {
396 size_t remaining;
397
398 log_debug("EOF reached with source %s (fd=%d)",
399 source->importer.name, source->importer.fd);
400
401 remaining = journal_importer_bytes_remaining(&source->importer);
402 if (remaining > 0)
403 log_notice("Premature EOF. %zu bytes lost.", remaining);
404 remove_source(s, source->importer.fd);
405 log_debug("%zu active sources remaining", s->active);
406 return 0;
407 } else if (r == -E2BIG) {
408 log_notice("Entry with too many fields, skipped");
409 return 1;
410 } else if (r == -ENOBUFS) {
411 log_notice("Entry too big, skipped");
412 return 1;
413 } else if (r == -EAGAIN) {
414 return 0;
415 } else if (r < 0) {
416 log_debug_errno(r, "Closing connection: %m");
417 remove_source(s, fd);
418 return 0;
419 } else
420 return 1;
421 }
422
423 static int dispatch_raw_source_until_block(sd_event_source *event,
424 void *userdata) {
425 RemoteSource *source = userdata;
426 int r;
427
428 /* Make sure event stays around even if source is destroyed */
429 sd_event_source_ref(event);
430
431 r = journal_remote_handle_raw_source(event, source->importer.fd, EPOLLIN, journal_remote_server_global);
432 if (r != 1)
433 /* No more data for now */
434 sd_event_source_set_enabled(event, SD_EVENT_OFF);
435
436 sd_event_source_unref(event);
437
438 return r;
439 }
440
441 static int dispatch_raw_source_event(sd_event_source *event,
442 int fd,
443 uint32_t revents,
444 void *userdata) {
445 RemoteSource *source = userdata;
446 int r;
447
448 assert(source->event);
449 assert(source->buffer_event);
450
451 r = journal_remote_handle_raw_source(event, fd, EPOLLIN, journal_remote_server_global);
452 if (r == 1)
453 /* Might have more data. We need to rerun the handler
454 * until we are sure the buffer is exhausted. */
455 sd_event_source_set_enabled(source->buffer_event, SD_EVENT_ON);
456
457 return r;
458 }
459
460 static int dispatch_blocking_source_event(sd_event_source *event,
461 void *userdata) {
462 RemoteSource *source = userdata;
463
464 return journal_remote_handle_raw_source(event, source->importer.fd, EPOLLIN, journal_remote_server_global);
465 }
466
467 static int accept_connection(
468 const char* type,
469 int fd,
470 SocketAddress *addr,
471 char **hostname) {
472
473 _cleanup_close_ int fd2 = -1;
474 int r;
475
476 log_debug("Accepting new %s connection on fd:%d", type, fd);
477 fd2 = accept4(fd, &addr->sockaddr.sa, &addr->size, SOCK_NONBLOCK|SOCK_CLOEXEC);
478 if (fd2 < 0) {
479 if (ERRNO_IS_ACCEPT_AGAIN(errno))
480 return -EAGAIN;
481
482 return log_error_errno(errno, "accept() on fd:%d failed: %m", fd);
483 }
484
485 switch(socket_address_family(addr)) {
486 case AF_INET:
487 case AF_INET6: {
488 _cleanup_free_ char *a = NULL;
489 char *b;
490
491 r = socket_address_print(addr, &a);
492 if (r < 0)
493 return log_error_errno(r, "socket_address_print(): %m");
494
495 r = socknameinfo_pretty(&addr->sockaddr, addr->size, &b);
496 if (r < 0)
497 return log_error_errno(r, "Resolving hostname failed: %m");
498
499 log_debug("Accepted %s %s connection from %s",
500 type,
501 af_to_ipv4_ipv6(socket_address_family(addr)),
502 a);
503
504 *hostname = b;
505 return TAKE_FD(fd2);
506 }
507
508 default:
509 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
510 "Rejected %s connection with unsupported family %d",
511 type, socket_address_family(addr));
512 }
513 }
514
515 static int dispatch_raw_connection_event(
516 sd_event_source *event,
517 int fd,
518 uint32_t revents,
519 void *userdata) {
520
521 RemoteServer *s = userdata;
522 int fd2;
523 SocketAddress addr = {
524 .size = sizeof(union sockaddr_union),
525 .type = SOCK_STREAM,
526 };
527 char *hostname = NULL;
528
529 fd2 = accept_connection("raw", fd, &addr, &hostname);
530 if (fd2 == -EAGAIN)
531 return 0;
532 if (fd2 < 0)
533 return fd2;
534
535 return journal_remote_add_source(s, fd2, hostname, true);
536 }