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), "/ ")
33 static int open_output(RemoteServer
*s
, Writer
*w
, const char* host
) {
34 _cleanup_free_
char *_filename
= NULL
;
38 switch (s
->split_mode
) {
39 case JOURNAL_WRITE_SPLIT_NONE
:
43 case JOURNAL_WRITE_SPLIT_HOST
: {
44 _cleanup_free_
char *name
= NULL
;
48 name
= filename_escape(host
);
52 r
= asprintf(&_filename
, "%s/remote-%s.journal", s
->output
, name
);
64 r
= managed_journal_file_open_reliably(
76 return log_error_errno(r
, "Failed to open output journal %s: %m", filename
);
78 log_debug("Opened output file %s", w
->journal
->file
->path
);
82 /**********************************************************************
83 **********************************************************************
84 **********************************************************************/
86 static int init_writer_hashmap(RemoteServer
*s
) {
87 static const struct hash_ops
* const hash_ops
[] = {
88 [JOURNAL_WRITE_SPLIT_NONE
] = NULL
,
89 [JOURNAL_WRITE_SPLIT_HOST
] = &string_hash_ops
,
93 assert(s
->split_mode
>= 0 && s
->split_mode
< (int) ELEMENTSOF(hash_ops
));
95 s
->writers
= hashmap_new(hash_ops
[s
->split_mode
]);
102 int journal_remote_get_writer(RemoteServer
*s
, const char *host
, Writer
**writer
) {
103 _cleanup_(writer_unrefp
) Writer
*w
= NULL
;
107 switch (s
->split_mode
) {
108 case JOURNAL_WRITE_SPLIT_NONE
:
109 key
= "one and only";
112 case JOURNAL_WRITE_SPLIT_HOST
:
118 assert_not_reached();
121 w
= hashmap_get(s
->writers
, key
);
129 if (s
->split_mode
== JOURNAL_WRITE_SPLIT_HOST
) {
130 w
->hashmap_key
= strdup(key
);
135 r
= open_output(s
, w
, host
);
139 r
= hashmap_put(s
->writers
, w
->hashmap_key
?: key
, w
);
144 *writer
= TAKE_PTR(w
);
149 /**********************************************************************
150 **********************************************************************
151 **********************************************************************/
153 /* This should go away as soon as µhttpd allows state to be passed around. */
154 RemoteServer
*journal_remote_server_global
;
156 static int dispatch_raw_source_event(sd_event_source
*event
,
160 static int dispatch_raw_source_until_block(sd_event_source
*event
,
162 static int dispatch_blocking_source_event(sd_event_source
*event
,
164 static int dispatch_raw_connection_event(sd_event_source
*event
,
169 static int get_source_for_fd(RemoteServer
*s
,
170 int fd
, char *name
, RemoteSource
**source
) {
174 /* This takes ownership of name, but only on success. */
179 if (!GREEDY_REALLOC0(s
->sources
, fd
+ 1))
182 r
= journal_remote_get_writer(s
, name
, &writer
);
184 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
187 if (!s
->sources
[fd
]) {
188 s
->sources
[fd
] = source_new(fd
, false, name
, writer
);
189 if (!s
->sources
[fd
]) {
190 writer_unref(writer
);
197 *source
= s
->sources
[fd
];
201 static int remove_source(RemoteServer
*s
, int fd
) {
202 RemoteSource
*source
;
205 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
207 source
= s
->sources
[fd
];
209 /* this closes fd too */
211 s
->sources
[fd
] = NULL
;
218 int journal_remote_add_source(RemoteServer
*s
, int fd
, char* name
, bool own_name
) {
219 RemoteSource
*source
= NULL
;
222 /* This takes ownership of name, even on failure, if own_name is true. */
234 r
= get_source_for_fd(s
, fd
, name
, &source
);
236 log_error_errno(r
, "Failed to create source for fd:%d (%s): %m",
242 r
= sd_event_add_io(s
->events
, &source
->event
,
243 fd
, EPOLLIN
|EPOLLRDHUP
|EPOLLPRI
,
244 dispatch_raw_source_event
, source
);
246 /* Add additional source for buffer processing. It will be
248 r
= sd_event_add_defer(s
->events
, &source
->buffer_event
,
249 dispatch_raw_source_until_block
, source
);
251 r
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_OFF
);
252 } else if (r
== -EPERM
) {
253 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd
, name
);
254 r
= sd_event_add_defer(s
->events
, &source
->event
,
255 dispatch_blocking_source_event
, source
);
257 r
= sd_event_source_set_enabled(source
->event
, SD_EVENT_ON
);
260 log_error_errno(r
, "Failed to register event source for fd:%d: %m",
265 r
= sd_event_source_set_description(source
->event
, name
);
267 log_error_errno(r
, "Failed to set source name for fd:%d: %m", fd
);
271 return 1; /* work to do */
274 remove_source(s
, fd
);
278 int journal_remote_add_raw_socket(RemoteServer
*s
, int fd
) {
280 _unused_ _cleanup_close_
int fd_
= fd
;
281 char name
[STRLEN("raw-socket-") + DECIMAL_STR_MAX(int) + 1];
285 r
= sd_event_add_io(s
->events
, &s
->listen_event
,
287 dispatch_raw_connection_event
, s
);
291 xsprintf(name
, "raw-socket-%d", fd
);
293 r
= sd_event_source_set_description(s
->listen_event
, name
);
302 /**********************************************************************
303 **********************************************************************
304 **********************************************************************/
306 int journal_remote_server_init(
309 JournalWriteSplitMode split_mode
,
310 JournalFileFlags file_flags
) {
316 assert(journal_remote_server_global
== NULL
);
317 journal_remote_server_global
= s
;
319 s
->split_mode
= split_mode
;
320 s
->file_flags
= file_flags
;
324 else if (split_mode
== JOURNAL_WRITE_SPLIT_NONE
)
325 s
->output
= REMOTE_JOURNAL_PATH
"/remote.journal";
326 else if (split_mode
== JOURNAL_WRITE_SPLIT_HOST
)
327 s
->output
= REMOTE_JOURNAL_PATH
;
329 assert_not_reached();
331 r
= sd_event_default(&s
->events
);
333 return log_error_errno(r
, "Failed to allocate event loop: %m");
335 r
= init_writer_hashmap(s
);
343 static void MHDDaemonWrapper_free(MHDDaemonWrapper
*d
) {
344 MHD_stop_daemon(d
->daemon
);
345 sd_event_source_unref(d
->io_event
);
346 sd_event_source_unref(d
->timer_event
);
351 void journal_remote_server_destroy(RemoteServer
*s
) {
355 hashmap_free_with_destructor(s
->daemons
, MHDDaemonWrapper_free
);
358 for (i
= 0; i
< MALLOC_ELEMENTSOF(s
->sources
); i
++)
362 writer_unref(s
->_single_writer
);
363 hashmap_free(s
->writers
);
365 sd_event_source_unref(s
->sigterm_event
);
366 sd_event_source_unref(s
->sigint_event
);
367 sd_event_source_unref(s
->listen_event
);
368 sd_event_unref(s
->events
);
370 if (s
== journal_remote_server_global
)
371 journal_remote_server_global
= NULL
;
373 /* fds that we're listening on remain open... */
376 /**********************************************************************
377 **********************************************************************
378 **********************************************************************/
380 int journal_remote_handle_raw_source(
381 sd_event_source
*event
,
386 RemoteSource
*source
;
389 /* Returns 1 if there might be more data pending,
390 * 0 if data is currently exhausted, negative on error.
393 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
394 source
= s
->sources
[fd
];
395 assert(source
->importer
.fd
== fd
);
397 r
= process_source(source
, s
->file_flags
);
398 if (journal_importer_eof(&source
->importer
)) {
401 log_debug("EOF reached with source %s (fd=%d)",
402 source
->importer
.name
, source
->importer
.fd
);
404 remaining
= journal_importer_bytes_remaining(&source
->importer
);
406 log_notice("Premature EOF. %zu bytes lost.", remaining
);
407 remove_source(s
, source
->importer
.fd
);
408 log_debug("%zu active sources remaining", s
->active
);
410 } else if (r
== -E2BIG
) {
411 log_notice("Entry with too many fields, skipped");
413 } else if (r
== -ENOBUFS
) {
414 log_notice("Entry too big, skipped");
416 } else if (r
== -EAGAIN
) {
419 log_debug_errno(r
, "Closing connection: %m");
420 remove_source(s
, fd
);
426 static int dispatch_raw_source_until_block(sd_event_source
*event
,
428 RemoteSource
*source
= userdata
;
431 /* Make sure event stays around even if source is destroyed */
432 sd_event_source_ref(event
);
434 r
= journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
438 /* No more data for now */
439 k
= sd_event_source_set_enabled(event
, SD_EVENT_OFF
);
444 sd_event_source_unref(event
);
449 static int dispatch_raw_source_event(sd_event_source
*event
,
453 RemoteSource
*source
= userdata
;
456 assert(source
->event
);
457 assert(source
->buffer_event
);
459 r
= journal_remote_handle_raw_source(event
, fd
, EPOLLIN
, journal_remote_server_global
);
463 /* Might have more data. We need to rerun the handler
464 * until we are sure the buffer is exhausted. */
465 k
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_ON
);
473 static int dispatch_blocking_source_event(sd_event_source
*event
,
475 RemoteSource
*source
= userdata
;
477 return journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
480 static int accept_connection(
486 _cleanup_close_
int fd2
= -1;
489 log_debug("Accepting new %s connection on fd:%d", type
, fd
);
490 fd2
= accept4(fd
, &addr
->sockaddr
.sa
, &addr
->size
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
492 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
495 return log_error_errno(errno
, "accept() on fd:%d failed: %m", fd
);
498 switch (socket_address_family(addr
)) {
501 _cleanup_free_
char *a
= NULL
;
504 r
= socket_address_print(addr
, &a
);
506 return log_error_errno(r
, "socket_address_print(): %m");
508 r
= socknameinfo_pretty(&addr
->sockaddr
, addr
->size
, &b
);
510 return log_error_errno(r
, "Resolving hostname failed: %m");
512 log_debug("Accepted %s %s connection from %s",
514 af_to_ipv4_ipv6(socket_address_family(addr
)),
522 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
523 "Rejected %s connection with unsupported family %d",
524 type
, socket_address_family(addr
));
528 static int dispatch_raw_connection_event(
529 sd_event_source
*event
,
534 RemoteServer
*s
= userdata
;
536 SocketAddress addr
= {
537 .size
= sizeof(union sockaddr_union
),
540 char *hostname
= NULL
;
542 fd2
= accept_connection("raw", fd
, &addr
, &hostname
);
548 return journal_remote_add_source(s
, fd2
, hostname
, true);