1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
12 #include "alloc-util.h"
13 #include "constants.h"
14 #include "errno-util.h"
17 #include "journal-remote-write.h"
18 #include "journal-remote.h"
19 #include "journald-native.h"
21 #include "managed-journal-file.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"
29 #define REMOTE_JOURNAL_PATH "/var/log/journal/remote"
31 #define filename_escape(s) xescape((s), "/ ")
34 MHDDaemonWrapper
*MHDDaemonWrapper_free(MHDDaemonWrapper
*d
) {
39 MHD_stop_daemon(d
->daemon
);
40 sd_event_source_unref(d
->io_event
);
41 sd_event_source_unref(d
->timer_event
);
47 static int open_output(RemoteServer
*s
, Writer
*w
, const char* host
) {
48 _cleanup_free_
char *_filename
= NULL
;
52 switch (s
->split_mode
) {
53 case JOURNAL_WRITE_SPLIT_NONE
:
57 case JOURNAL_WRITE_SPLIT_HOST
: {
58 _cleanup_free_
char *name
= NULL
;
62 name
= filename_escape(host
);
66 r
= asprintf(&_filename
, "%s/remote-%s.journal", s
->output
, name
);
78 r
= managed_journal_file_open_reliably(
90 return log_error_errno(r
, "Failed to open output journal %s: %m", filename
);
92 log_debug("Opened output file %s", w
->journal
->file
->path
);
96 /**********************************************************************
97 **********************************************************************
98 **********************************************************************/
100 static int init_writer_hashmap(RemoteServer
*s
) {
101 static const struct hash_ops
* const hash_ops
[] = {
102 [JOURNAL_WRITE_SPLIT_NONE
] = NULL
,
103 [JOURNAL_WRITE_SPLIT_HOST
] = &string_hash_ops
,
107 assert(s
->split_mode
>= 0 && s
->split_mode
< (int) ELEMENTSOF(hash_ops
));
109 s
->writers
= hashmap_new(hash_ops
[s
->split_mode
]);
116 int journal_remote_get_writer(RemoteServer
*s
, const char *host
, Writer
**writer
) {
117 _cleanup_(writer_unrefp
) Writer
*w
= NULL
;
121 switch (s
->split_mode
) {
122 case JOURNAL_WRITE_SPLIT_NONE
:
123 key
= "one and only";
126 case JOURNAL_WRITE_SPLIT_HOST
:
132 assert_not_reached();
135 w
= hashmap_get(s
->writers
, key
);
139 r
= writer_new(s
, &w
);
143 if (s
->split_mode
== JOURNAL_WRITE_SPLIT_HOST
) {
144 w
->hashmap_key
= strdup(key
);
149 r
= open_output(s
, w
, host
);
153 r
= hashmap_put(s
->writers
, w
->hashmap_key
?: key
, w
);
158 *writer
= TAKE_PTR(w
);
162 /**********************************************************************
163 **********************************************************************
164 **********************************************************************/
166 /* This should go away as soon as μhttpd allows state to be passed around. */
167 RemoteServer
*journal_remote_server_global
;
169 static int dispatch_raw_source_event(sd_event_source
*event
,
173 static int dispatch_raw_source_until_block(sd_event_source
*event
,
175 static int dispatch_blocking_source_event(sd_event_source
*event
,
177 static int dispatch_raw_connection_event(sd_event_source
*event
,
182 static int get_source_for_fd(RemoteServer
*s
,
183 int fd
, char *name
, RemoteSource
**source
) {
187 /* This takes ownership of name, but only on success. */
192 if (!GREEDY_REALLOC0(s
->sources
, fd
+ 1))
195 r
= journal_remote_get_writer(s
, name
, &writer
);
197 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
200 if (!s
->sources
[fd
]) {
201 s
->sources
[fd
] = source_new(fd
, false, name
, writer
);
202 if (!s
->sources
[fd
]) {
203 writer_unref(writer
);
210 *source
= s
->sources
[fd
];
214 static int remove_source(RemoteServer
*s
, int fd
) {
215 RemoteSource
*source
;
218 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
220 source
= s
->sources
[fd
];
222 /* this closes fd too */
224 s
->sources
[fd
] = NULL
;
231 int journal_remote_add_source(RemoteServer
*s
, int fd
, char* name
, bool own_name
) {
232 RemoteSource
*source
= NULL
;
235 /* This takes ownership of name, even on failure, if own_name is true. */
247 r
= get_source_for_fd(s
, fd
, name
, &source
);
249 log_error_errno(r
, "Failed to create source for fd:%d (%s): %m",
255 r
= sd_event_add_io(s
->events
, &source
->event
,
256 fd
, EPOLLIN
|EPOLLRDHUP
|EPOLLPRI
,
257 dispatch_raw_source_event
, source
);
259 /* Add additional source for buffer processing. It will be
261 r
= sd_event_add_defer(s
->events
, &source
->buffer_event
,
262 dispatch_raw_source_until_block
, source
);
264 r
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_OFF
);
265 } else if (r
== -EPERM
) {
266 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd
, name
);
267 r
= sd_event_add_defer(s
->events
, &source
->event
,
268 dispatch_blocking_source_event
, source
);
270 r
= sd_event_source_set_enabled(source
->event
, SD_EVENT_ON
);
273 log_error_errno(r
, "Failed to register event source for fd:%d: %m",
278 r
= sd_event_source_set_description(source
->event
, name
);
280 log_error_errno(r
, "Failed to set source name for fd:%d: %m", fd
);
284 return 1; /* work to do */
287 remove_source(s
, fd
);
291 int journal_remote_add_raw_socket(RemoteServer
*s
, int fd
) {
293 _unused_ _cleanup_close_
int fd_
= fd
;
294 char name
[STRLEN("raw-socket-") + DECIMAL_STR_MAX(int) + 1];
298 r
= sd_event_add_io(s
->events
, &s
->listen_event
,
300 dispatch_raw_connection_event
, s
);
304 xsprintf(name
, "raw-socket-%d", fd
);
306 r
= sd_event_source_set_description(s
->listen_event
, name
);
315 /**********************************************************************
316 **********************************************************************
317 **********************************************************************/
319 int journal_remote_server_init(
322 JournalWriteSplitMode split_mode
,
323 JournalFileFlags file_flags
) {
329 assert(journal_remote_server_global
== NULL
);
330 journal_remote_server_global
= s
;
332 s
->split_mode
= split_mode
;
333 s
->file_flags
= file_flags
;
337 else if (split_mode
== JOURNAL_WRITE_SPLIT_NONE
)
338 s
->output
= REMOTE_JOURNAL_PATH
"/remote.journal";
339 else if (split_mode
== JOURNAL_WRITE_SPLIT_HOST
)
340 s
->output
= REMOTE_JOURNAL_PATH
;
342 assert_not_reached();
344 r
= sd_event_default(&s
->events
);
346 return log_error_errno(r
, "Failed to allocate event loop: %m");
348 r
= init_writer_hashmap(s
);
355 void journal_remote_server_destroy(RemoteServer
*s
) {
359 hashmap_free_with_destructor(s
->daemons
, MHDDaemonWrapper_free
);
362 for (i
= 0; i
< MALLOC_ELEMENTSOF(s
->sources
); i
++)
366 writer_unref(s
->_single_writer
);
367 hashmap_free(s
->writers
);
369 sd_event_source_unref(s
->sigterm_event
);
370 sd_event_source_unref(s
->sigint_event
);
371 sd_event_source_unref(s
->listen_event
);
372 sd_event_unref(s
->events
);
374 if (s
== journal_remote_server_global
)
375 journal_remote_server_global
= NULL
;
377 /* fds that we're listening on remain open... */
380 /**********************************************************************
381 **********************************************************************
382 **********************************************************************/
384 int journal_remote_handle_raw_source(
385 sd_event_source
*event
,
390 RemoteSource
*source
;
393 /* Returns 1 if there might be more data pending,
394 * 0 if data is currently exhausted, negative on error.
397 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
398 source
= s
->sources
[fd
];
399 assert(source
->importer
.fd
== fd
);
401 r
= process_source(source
, s
->file_flags
);
402 if (journal_importer_eof(&source
->importer
)) {
405 log_debug("EOF reached with source %s (fd=%d)",
406 source
->importer
.name
, source
->importer
.fd
);
408 remaining
= journal_importer_bytes_remaining(&source
->importer
);
410 log_notice("Premature EOF. %zu bytes lost.", remaining
);
411 remove_source(s
, source
->importer
.fd
);
412 log_debug("%zu active sources remaining", s
->active
);
414 } else if (r
== -E2BIG
) {
415 log_notice("Entry with too many fields, skipped");
417 } else if (r
== -ENOBUFS
) {
418 log_notice("Entry too big, skipped");
420 } else if (r
== -EAGAIN
) {
423 log_debug_errno(r
, "Closing connection: %m");
424 remove_source(s
, fd
);
430 static int dispatch_raw_source_until_block(sd_event_source
*event
,
432 RemoteSource
*source
= userdata
;
435 /* Make sure event stays around even if source is destroyed */
436 sd_event_source_ref(event
);
438 r
= journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
442 /* No more data for now */
443 k
= sd_event_source_set_enabled(event
, SD_EVENT_OFF
);
448 sd_event_source_unref(event
);
453 static int dispatch_raw_source_event(sd_event_source
*event
,
457 RemoteSource
*source
= userdata
;
460 assert(source
->event
);
461 assert(source
->buffer_event
);
463 r
= journal_remote_handle_raw_source(event
, fd
, EPOLLIN
, journal_remote_server_global
);
467 /* Might have more data. We need to rerun the handler
468 * until we are sure the buffer is exhausted. */
469 k
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_ON
);
477 static int dispatch_blocking_source_event(sd_event_source
*event
,
479 RemoteSource
*source
= userdata
;
481 return journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
484 static int accept_connection(
490 _cleanup_close_
int fd2
= -EBADF
;
493 log_debug("Accepting new %s connection on fd:%d", type
, fd
);
494 fd2
= accept4(fd
, &addr
->sockaddr
.sa
, &addr
->size
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
496 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
499 return log_error_errno(errno
, "accept() on fd:%d failed: %m", fd
);
502 switch (socket_address_family(addr
)) {
505 _cleanup_free_
char *a
= NULL
;
508 r
= socket_address_print(addr
, &a
);
510 return log_error_errno(r
, "socket_address_print(): %m");
512 r
= socknameinfo_pretty(&addr
->sockaddr
, addr
->size
, &b
);
514 return log_error_errno(r
, "Resolving hostname failed: %m");
516 log_debug("Accepted %s %s connection from %s",
518 af_to_ipv4_ipv6(socket_address_family(addr
)),
526 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
527 "Rejected %s connection with unsupported family %d",
528 type
, socket_address_family(addr
));
532 static int dispatch_raw_connection_event(
533 sd_event_source
*event
,
538 RemoteServer
*s
= userdata
;
540 SocketAddress addr
= {
541 .size
= sizeof(union sockaddr_union
),
544 char *hostname
= NULL
;
546 fd2
= accept_connection("raw", fd
, &addr
, &hostname
);
552 return journal_remote_add_source(s
, fd2
, hostname
, true);