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-file-util.h"
18 #include "journal-remote-write.h"
19 #include "journal-remote.h"
21 #include "parse-util.h"
22 #include "parse-helpers.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
;
55 switch (s
->split_mode
) {
56 case JOURNAL_WRITE_SPLIT_NONE
:
60 case JOURNAL_WRITE_SPLIT_HOST
: {
61 _cleanup_free_
char *name
= NULL
;
65 name
= filename_escape(host
);
69 r
= asprintf(&_filename
, "%s/remote-%s.journal", s
->output
, name
);
81 r
= journal_file_open_reliably(
91 return log_error_errno(r
, "Failed to open output journal %s: %m", filename
);
93 log_debug("Opened output file %s", w
->journal
->path
);
97 /**********************************************************************
98 **********************************************************************
99 **********************************************************************/
101 static int init_writer_hashmap(RemoteServer
*s
) {
102 static const struct hash_ops
* const hash_ops
[] = {
103 [JOURNAL_WRITE_SPLIT_NONE
] = NULL
,
104 [JOURNAL_WRITE_SPLIT_HOST
] = &string_hash_ops
,
108 assert(s
->split_mode
>= 0 && s
->split_mode
< (int) ELEMENTSOF(hash_ops
));
110 s
->writers
= hashmap_new(hash_ops
[s
->split_mode
]);
117 int journal_remote_get_writer(RemoteServer
*s
, const char *host
, Writer
**writer
) {
118 _cleanup_(writer_unrefp
) Writer
*w
= NULL
;
125 switch (s
->split_mode
) {
126 case JOURNAL_WRITE_SPLIT_NONE
:
127 key
= "one and only";
130 case JOURNAL_WRITE_SPLIT_HOST
:
136 assert_not_reached();
139 w
= hashmap_get(s
->writers
, key
);
143 r
= writer_new(s
, &w
);
147 if (s
->split_mode
== JOURNAL_WRITE_SPLIT_HOST
) {
148 w
->hashmap_key
= strdup(key
);
153 r
= open_output(s
, w
, host
);
157 r
= hashmap_put(s
->writers
, w
->hashmap_key
?: key
, w
);
162 *writer
= TAKE_PTR(w
);
166 /**********************************************************************
167 **********************************************************************
168 **********************************************************************/
170 /* This should go away as soon as μhttpd allows state to be passed around. */
171 RemoteServer
*journal_remote_server_global
;
173 static int dispatch_raw_source_event(sd_event_source
*event
,
177 static int dispatch_raw_source_until_block(sd_event_source
*event
,
179 static int dispatch_blocking_source_event(sd_event_source
*event
,
181 static int dispatch_raw_connection_event(sd_event_source
*event
,
186 static int get_source_for_fd(RemoteServer
*s
,
187 int fd
, char *name
, RemoteSource
**source
) {
191 /* This takes ownership of name, but only on success. */
197 if (!GREEDY_REALLOC0(s
->sources
, fd
+ 1))
200 r
= journal_remote_get_writer(s
, name
, &writer
);
202 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
205 if (!s
->sources
[fd
]) {
206 s
->sources
[fd
] = source_new(fd
, false, name
, writer
);
207 if (!s
->sources
[fd
]) {
208 writer_unref(writer
);
215 *source
= s
->sources
[fd
];
219 static int remove_source(RemoteServer
*s
, int fd
) {
220 RemoteSource
*source
;
223 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
225 source
= s
->sources
[fd
];
227 /* this closes fd too */
229 s
->sources
[fd
] = NULL
;
236 int journal_remote_add_source(RemoteServer
*s
, int fd
, char* name
, bool own_name
) {
237 RemoteSource
*source
= NULL
;
240 /* This takes ownership of name, even on failure, if own_name is true. */
252 r
= get_source_for_fd(s
, fd
, name
, &source
);
254 log_error_errno(r
, "Failed to create source for fd:%d (%s): %m",
260 r
= sd_event_add_io(s
->event
, &source
->event
,
261 fd
, EPOLLIN
|EPOLLRDHUP
|EPOLLPRI
,
262 dispatch_raw_source_event
, source
);
264 /* Add additional source for buffer processing. It will be
266 r
= sd_event_add_defer(s
->event
, &source
->buffer_event
,
267 dispatch_raw_source_until_block
, source
);
269 r
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_OFF
);
270 } else if (r
== -EPERM
) {
271 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd
, name
);
272 r
= sd_event_add_defer(s
->event
, &source
->event
,
273 dispatch_blocking_source_event
, source
);
275 r
= sd_event_source_set_enabled(source
->event
, SD_EVENT_ON
);
278 log_error_errno(r
, "Failed to register event source for fd:%d: %m",
283 r
= sd_event_source_set_description(source
->event
, name
);
285 log_error_errno(r
, "Failed to set source name for fd:%d: %m", fd
);
289 return 1; /* work to do */
292 remove_source(s
, fd
);
296 int journal_remote_add_raw_socket(RemoteServer
*s
, int fd
) {
297 _unused_ _cleanup_close_
int fd_
= fd
;
298 char name
[STRLEN("raw-socket-") + DECIMAL_STR_MAX(int) + 1];
304 r
= sd_event_add_io(s
->event
, &s
->listen_event
,
306 dispatch_raw_connection_event
, s
);
310 xsprintf(name
, "raw-socket-%d", fd
);
312 r
= sd_event_source_set_description(s
->listen_event
, name
);
321 /**********************************************************************
322 **********************************************************************
323 **********************************************************************/
325 int journal_remote_server_init(
328 JournalWriteSplitMode split_mode
,
329 JournalFileFlags file_flags
) {
335 assert(journal_remote_server_global
== NULL
);
336 journal_remote_server_global
= s
;
338 s
->split_mode
= split_mode
;
339 s
->file_flags
= file_flags
;
343 else if (split_mode
== JOURNAL_WRITE_SPLIT_NONE
)
344 s
->output
= REMOTE_JOURNAL_PATH
"/remote.journal";
345 else if (split_mode
== JOURNAL_WRITE_SPLIT_HOST
)
346 s
->output
= REMOTE_JOURNAL_PATH
;
348 assert_not_reached();
350 r
= sd_event_default(&s
->event
);
352 return log_error_errno(r
, "Failed to allocate event loop: %m");
354 r
= init_writer_hashmap(s
);
361 void journal_remote_server_destroy(RemoteServer
*s
) {
368 hashmap_free_with_destructor(s
->daemons
, MHDDaemonWrapper_free
);
371 for (i
= 0; i
< MALLOC_ELEMENTSOF(s
->sources
); i
++)
375 writer_unref(s
->_single_writer
);
376 hashmap_free(s
->writers
);
378 sd_event_source_unref(s
->listen_event
);
379 sd_event_unref(s
->event
);
381 if (s
== journal_remote_server_global
)
382 journal_remote_server_global
= NULL
;
384 /* fds that we're listening on remain open... */
387 /**********************************************************************
388 **********************************************************************
389 **********************************************************************/
391 int journal_remote_handle_raw_source(
392 sd_event_source
*event
,
397 RemoteSource
*source
;
400 /* Returns 1 if there might be more data pending,
401 * 0 if data is currently exhausted, negative on error.
405 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
406 source
= s
->sources
[fd
];
407 assert(source
->importer
.fd
== fd
);
409 r
= process_source(source
, s
->file_flags
);
410 if (journal_importer_eof(&source
->importer
)) {
413 log_debug("EOF reached with source %s (fd=%d)",
414 source
->importer
.name
, source
->importer
.fd
);
416 remaining
= journal_importer_bytes_remaining(&source
->importer
);
418 log_notice("Premature EOF. %zu bytes lost.", remaining
);
419 remove_source(s
, source
->importer
.fd
);
420 log_debug("%zu active sources remaining", s
->active
);
422 } else if (r
== -E2BIG
) {
423 log_notice("Entry with too many fields, skipped");
425 } else if (r
== -ENOBUFS
) {
426 log_notice("Entry too big, skipped");
428 } else if (r
== -EAGAIN
) {
431 log_debug_errno(r
, "Closing connection: %m");
432 remove_source(s
, fd
);
438 static int dispatch_raw_source_until_block(sd_event_source
*event
,
440 RemoteSource
*source
= ASSERT_PTR(userdata
);
445 /* Make sure event stays around even if source is destroyed */
446 sd_event_source_ref(event
);
448 r
= journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
452 /* No more data for now */
453 k
= sd_event_source_set_enabled(event
, SD_EVENT_OFF
);
458 sd_event_source_unref(event
);
463 static int dispatch_raw_source_event(sd_event_source
*event
,
467 RemoteSource
*source
= ASSERT_PTR(userdata
);
470 assert(source
->event
);
471 assert(source
->buffer_event
);
473 r
= journal_remote_handle_raw_source(event
, fd
, EPOLLIN
, journal_remote_server_global
);
477 /* Might have more data. We need to rerun the handler
478 * until we are sure the buffer is exhausted. */
479 k
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_ON
);
487 static int dispatch_blocking_source_event(sd_event_source
*event
,
489 RemoteSource
*source
= ASSERT_PTR(userdata
);
491 return journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
494 static int accept_connection(
500 _cleanup_close_
int fd2
= -EBADF
;
506 log_debug("Accepting new %s connection on fd:%d", type
, fd
);
507 fd2
= accept4(fd
, &addr
->sockaddr
.sa
, &addr
->size
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
509 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
512 return log_error_errno(errno
, "accept() on fd:%d failed: %m", fd
);
515 switch (socket_address_family(addr
)) {
520 _cleanup_free_
char *a
= NULL
;
523 r
= socket_address_print(addr
, &a
);
525 return log_error_errno(r
, "socket_address_print(): %m");
527 r
= socknameinfo_pretty(&addr
->sockaddr
.sa
, addr
->size
, &b
);
529 return log_error_errno(r
, "Resolving hostname failed: %m");
531 log_debug("Accepted %s %s connection from %s",
533 af_to_ipv4_ipv6(socket_address_family(addr
)),
541 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
542 "Rejected %s connection with unsupported family %d",
543 type
, socket_address_family(addr
));
547 static int dispatch_raw_connection_event(
548 sd_event_source
*event
,
553 RemoteServer
*s
= ASSERT_PTR(userdata
);
555 SocketAddress addr
= {
556 .size
= sizeof(union sockaddr_union
),
559 char *hostname
= NULL
;
561 fd2
= accept_connection("raw", fd
, &addr
, &hostname
);
567 return journal_remote_add_source(s
, fd2
, hostname
, true);