1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include <sys/socket.h>
12 #include "sd-daemon.h"
14 #include "alloc-util.h"
18 #include "journal-file.h"
19 #include "journal-remote-write.h"
20 #include "journal-remote.h"
21 #include "journald-native.h"
23 #include "parse-util.h"
24 #include "process-util.h"
25 #include "socket-util.h"
26 #include "stdio-util.h"
27 #include "string-util.h"
30 #define REMOTE_JOURNAL_PATH "/var/log/journal/remote"
32 #define filename_escape(s) xescape((s), "/ ")
34 static int open_output(RemoteServer
*s
, Writer
*w
, const char* host
) {
35 _cleanup_free_
char *_filename
= NULL
;
39 switch (s
->split_mode
) {
40 case JOURNAL_WRITE_SPLIT_NONE
:
44 case JOURNAL_WRITE_SPLIT_HOST
: {
45 _cleanup_free_
char *name
;
49 name
= filename_escape(host
);
53 r
= asprintf(&_filename
, "%s/remote-%s.journal", s
->output
, name
);
62 assert_not_reached("what?");
65 r
= journal_file_open_reliably(filename
,
67 s
->compress
, (uint64_t) -1, s
->seal
,
72 return log_error_errno(r
, "Failed to open output journal %s: %m", filename
);
74 log_debug("Opened output file %s", w
->journal
->path
);
78 /**********************************************************************
79 **********************************************************************
80 **********************************************************************/
82 static int init_writer_hashmap(RemoteServer
*s
) {
83 static const struct hash_ops
* const hash_ops
[] = {
84 [JOURNAL_WRITE_SPLIT_NONE
] = NULL
,
85 [JOURNAL_WRITE_SPLIT_HOST
] = &string_hash_ops
,
89 assert(s
->split_mode
>= 0 && s
->split_mode
< (int) ELEMENTSOF(hash_ops
));
91 s
->writers
= hashmap_new(hash_ops
[s
->split_mode
]);
98 int journal_remote_get_writer(RemoteServer
*s
, const char *host
, Writer
**writer
) {
99 _cleanup_(writer_unrefp
) Writer
*w
= NULL
;
103 switch(s
->split_mode
) {
104 case JOURNAL_WRITE_SPLIT_NONE
:
105 key
= "one and only";
108 case JOURNAL_WRITE_SPLIT_HOST
:
114 assert_not_reached("what split mode?");
117 w
= hashmap_get(s
->writers
, key
);
125 if (s
->split_mode
== JOURNAL_WRITE_SPLIT_HOST
) {
126 w
->hashmap_key
= strdup(key
);
131 r
= open_output(s
, w
, host
);
135 r
= hashmap_put(s
->writers
, w
->hashmap_key
?: key
, w
);
140 *writer
= TAKE_PTR(w
);
145 /**********************************************************************
146 **********************************************************************
147 **********************************************************************/
149 /* This should go away as soon as µhttpd allows state to be passed around. */
150 RemoteServer
*journal_remote_server_global
;
152 static int dispatch_raw_source_event(sd_event_source
*event
,
156 static int dispatch_raw_source_until_block(sd_event_source
*event
,
158 static int dispatch_blocking_source_event(sd_event_source
*event
,
160 static int dispatch_raw_connection_event(sd_event_source
*event
,
165 static int get_source_for_fd(RemoteServer
*s
,
166 int fd
, char *name
, RemoteSource
**source
) {
170 /* This takes ownership of name, but only on success. */
175 if (!GREEDY_REALLOC0(s
->sources
, s
->sources_size
, fd
+ 1))
178 r
= journal_remote_get_writer(s
, name
, &writer
);
180 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
183 if (s
->sources
[fd
] == NULL
) {
184 s
->sources
[fd
] = source_new(fd
, false, name
, writer
);
185 if (!s
->sources
[fd
]) {
186 writer_unref(writer
);
193 *source
= s
->sources
[fd
];
197 static int remove_source(RemoteServer
*s
, int fd
) {
198 RemoteSource
*source
;
201 assert(fd
>= 0 && fd
< (ssize_t
) s
->sources_size
);
203 source
= s
->sources
[fd
];
205 /* this closes fd too */
207 s
->sources
[fd
] = NULL
;
214 int journal_remote_add_source(RemoteServer
*s
, int fd
, char* name
, bool own_name
) {
215 RemoteSource
*source
= NULL
;
218 /* This takes ownership of name, even on failure, if own_name is true. */
230 r
= get_source_for_fd(s
, fd
, name
, &source
);
232 log_error_errno(r
, "Failed to create source for fd:%d (%s): %m",
238 r
= sd_event_add_io(s
->events
, &source
->event
,
239 fd
, EPOLLIN
|EPOLLRDHUP
|EPOLLPRI
,
240 dispatch_raw_source_event
, source
);
242 /* Add additional source for buffer processing. It will be
244 r
= sd_event_add_defer(s
->events
, &source
->buffer_event
,
245 dispatch_raw_source_until_block
, source
);
247 sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_OFF
);
248 } else if (r
== -EPERM
) {
249 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd
, name
);
250 r
= sd_event_add_defer(s
->events
, &source
->event
,
251 dispatch_blocking_source_event
, source
);
253 sd_event_source_set_enabled(source
->event
, SD_EVENT_ON
);
256 log_error_errno(r
, "Failed to register event source for fd:%d: %m",
261 r
= sd_event_source_set_description(source
->event
, name
);
263 log_error_errno(r
, "Failed to set source name for fd:%d: %m", fd
);
267 return 1; /* work to do */
270 remove_source(s
, fd
);
274 int journal_remote_add_raw_socket(RemoteServer
*s
, int fd
) {
276 _cleanup_close_
int fd_
= fd
;
277 char name
[STRLEN("raw-socket-") + DECIMAL_STR_MAX(int) + 1];
281 r
= sd_event_add_io(s
->events
, &s
->listen_event
,
283 dispatch_raw_connection_event
, s
);
287 xsprintf(name
, "raw-socket-%d", fd
);
289 r
= sd_event_source_set_description(s
->listen_event
, name
);
298 /**********************************************************************
299 **********************************************************************
300 **********************************************************************/
302 int journal_remote_server_init(
305 JournalWriteSplitMode split_mode
,
313 assert(journal_remote_server_global
== NULL
);
314 journal_remote_server_global
= s
;
316 s
->split_mode
= split_mode
;
317 s
->compress
= compress
;
322 else if (split_mode
== JOURNAL_WRITE_SPLIT_NONE
)
323 s
->output
= REMOTE_JOURNAL_PATH
"/remote.journal";
324 else if (split_mode
== JOURNAL_WRITE_SPLIT_HOST
)
325 s
->output
= REMOTE_JOURNAL_PATH
;
327 assert_not_reached("bad split mode");
329 r
= sd_event_default(&s
->events
);
331 return log_error_errno(r
, "Failed to allocate event loop: %m");
333 r
= init_writer_hashmap(s
);
341 static void MHDDaemonWrapper_free(MHDDaemonWrapper
*d
) {
342 MHD_stop_daemon(d
->daemon
);
343 sd_event_source_unref(d
->io_event
);
344 sd_event_source_unref(d
->timer_event
);
349 void journal_remote_server_destroy(RemoteServer
*s
) {
353 hashmap_free_with_destructor(s
->daemons
, MHDDaemonWrapper_free
);
356 assert(s
->sources_size
== 0 || s
->sources
);
357 for (i
= 0; i
< s
->sources_size
; i
++)
361 writer_unref(s
->_single_writer
);
362 hashmap_free(s
->writers
);
364 sd_event_source_unref(s
->sigterm_event
);
365 sd_event_source_unref(s
->sigint_event
);
366 sd_event_source_unref(s
->listen_event
);
367 sd_event_unref(s
->events
);
369 if (s
== journal_remote_server_global
)
370 journal_remote_server_global
= NULL
;
372 /* fds that we're listening on remain open... */
375 /**********************************************************************
376 **********************************************************************
377 **********************************************************************/
379 int journal_remote_handle_raw_source(
380 sd_event_source
*event
,
385 RemoteSource
*source
;
388 /* Returns 1 if there might be more data pending,
389 * 0 if data is currently exhausted, negative on error.
392 assert(fd
>= 0 && fd
< (ssize_t
) s
->sources_size
);
393 source
= s
->sources
[fd
];
394 assert(source
->importer
.fd
== fd
);
396 r
= process_source(source
, s
->compress
, s
->seal
);
397 if (journal_importer_eof(&source
->importer
)) {
400 log_debug("EOF reached with source %s (fd=%d)",
401 source
->importer
.name
, source
->importer
.fd
);
403 remaining
= journal_importer_bytes_remaining(&source
->importer
);
405 log_notice("Premature EOF. %zu bytes lost.", remaining
);
406 remove_source(s
, source
->importer
.fd
);
407 log_debug("%zu active sources remaining", s
->active
);
409 } else if (r
== -E2BIG
) {
410 log_notice("Entry with too many fields, skipped");
412 } else if (r
== -ENOBUFS
) {
413 log_notice("Entry too big, skipped");
415 } else if (r
== -EAGAIN
) {
418 log_debug_errno(r
, "Closing connection: %m");
419 remove_source(s
, fd
);
425 static int dispatch_raw_source_until_block(sd_event_source
*event
,
427 RemoteSource
*source
= userdata
;
430 /* Make sure event stays around even if source is destroyed */
431 sd_event_source_ref(event
);
433 r
= journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
435 /* No more data for now */
436 sd_event_source_set_enabled(event
, SD_EVENT_OFF
);
438 sd_event_source_unref(event
);
443 static int dispatch_raw_source_event(sd_event_source
*event
,
447 RemoteSource
*source
= userdata
;
450 assert(source
->event
);
451 assert(source
->buffer_event
);
453 r
= journal_remote_handle_raw_source(event
, fd
, EPOLLIN
, journal_remote_server_global
);
455 /* Might have more data. We need to rerun the handler
456 * until we are sure the buffer is exhausted. */
457 sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_ON
);
462 static int dispatch_blocking_source_event(sd_event_source
*event
,
464 RemoteSource
*source
= userdata
;
466 return journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
469 static int accept_connection(const char* type
, int fd
,
470 SocketAddress
*addr
, char **hostname
) {
473 log_debug("Accepting new %s connection on fd:%d", type
, fd
);
474 fd2
= accept4(fd
, &addr
->sockaddr
.sa
, &addr
->size
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
476 return log_error_errno(errno
, "accept() on fd:%d failed: %m", fd
);
478 switch(socket_address_family(addr
)) {
481 _cleanup_free_
char *a
= NULL
;
484 r
= socket_address_print(addr
, &a
);
486 log_error_errno(r
, "socket_address_print(): %m");
491 r
= socknameinfo_pretty(&addr
->sockaddr
, addr
->size
, &b
);
493 log_error_errno(r
, "Resolving hostname failed: %m");
498 log_debug("Accepted %s %s connection from %s",
500 socket_address_family(addr
) == AF_INET
? "IP" : "IPv6",
508 log_error("Rejected %s connection with unsupported family %d",
509 type
, socket_address_family(addr
));
516 static int dispatch_raw_connection_event(sd_event_source
*event
,
520 RemoteServer
*s
= userdata
;
522 SocketAddress addr
= {
523 .size
= sizeof(union sockaddr_union
),
526 char *hostname
= NULL
;
528 fd2
= accept_connection("raw", fd
, &addr
, &hostname
);
532 return journal_remote_add_source(s
, fd2
, hostname
, true);