1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include <sys/socket.h>
12 #include "sd-daemon.h"
14 #include "alloc-util.h"
16 #include "errno-util.h"
19 #include "journal-file.h"
20 #include "journal-remote-write.h"
21 #include "journal-remote.h"
22 #include "journald-native.h"
24 #include "parse-util.h"
25 #include "process-util.h"
26 #include "socket-util.h"
27 #include "stdio-util.h"
28 #include "string-util.h"
31 #define REMOTE_JOURNAL_PATH "/var/log/journal/remote"
33 #define filename_escape(s) xescape((s), "/ ")
35 static int open_output(RemoteServer
*s
, Writer
*w
, const char* host
) {
36 _cleanup_free_
char *_filename
= NULL
;
40 switch (s
->split_mode
) {
41 case JOURNAL_WRITE_SPLIT_NONE
:
45 case JOURNAL_WRITE_SPLIT_HOST
: {
46 _cleanup_free_
char *name
;
50 name
= filename_escape(host
);
54 r
= asprintf(&_filename
, "%s/remote-%s.journal", s
->output
, name
);
63 assert_not_reached("what?");
66 r
= journal_file_open_reliably(filename
,
68 s
->compress
, (uint64_t) -1, s
->seal
,
73 return log_error_errno(r
, "Failed to open output journal %s: %m", filename
);
75 log_debug("Opened output file %s", w
->journal
->path
);
79 /**********************************************************************
80 **********************************************************************
81 **********************************************************************/
83 static int init_writer_hashmap(RemoteServer
*s
) {
84 static const struct hash_ops
* const hash_ops
[] = {
85 [JOURNAL_WRITE_SPLIT_NONE
] = NULL
,
86 [JOURNAL_WRITE_SPLIT_HOST
] = &string_hash_ops
,
90 assert(s
->split_mode
>= 0 && s
->split_mode
< (int) ELEMENTSOF(hash_ops
));
92 s
->writers
= hashmap_new(hash_ops
[s
->split_mode
]);
99 int journal_remote_get_writer(RemoteServer
*s
, const char *host
, Writer
**writer
) {
100 _cleanup_(writer_unrefp
) Writer
*w
= NULL
;
104 switch(s
->split_mode
) {
105 case JOURNAL_WRITE_SPLIT_NONE
:
106 key
= "one and only";
109 case JOURNAL_WRITE_SPLIT_HOST
:
115 assert_not_reached("what split mode?");
118 w
= hashmap_get(s
->writers
, key
);
126 if (s
->split_mode
== JOURNAL_WRITE_SPLIT_HOST
) {
127 w
->hashmap_key
= strdup(key
);
132 r
= open_output(s
, w
, host
);
136 r
= hashmap_put(s
->writers
, w
->hashmap_key
?: key
, w
);
141 *writer
= TAKE_PTR(w
);
146 /**********************************************************************
147 **********************************************************************
148 **********************************************************************/
150 /* This should go away as soon as µhttpd allows state to be passed around. */
151 RemoteServer
*journal_remote_server_global
;
153 static int dispatch_raw_source_event(sd_event_source
*event
,
157 static int dispatch_raw_source_until_block(sd_event_source
*event
,
159 static int dispatch_blocking_source_event(sd_event_source
*event
,
161 static int dispatch_raw_connection_event(sd_event_source
*event
,
166 static int get_source_for_fd(RemoteServer
*s
,
167 int fd
, char *name
, RemoteSource
**source
) {
171 /* This takes ownership of name, but only on success. */
176 if (!GREEDY_REALLOC0(s
->sources
, s
->sources_size
, fd
+ 1))
179 r
= journal_remote_get_writer(s
, name
, &writer
);
181 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
184 if (!s
->sources
[fd
]) {
185 s
->sources
[fd
] = source_new(fd
, false, name
, writer
);
186 if (!s
->sources
[fd
]) {
187 writer_unref(writer
);
194 *source
= s
->sources
[fd
];
198 static int remove_source(RemoteServer
*s
, int fd
) {
199 RemoteSource
*source
;
202 assert(fd
>= 0 && fd
< (ssize_t
) s
->sources_size
);
204 source
= s
->sources
[fd
];
206 /* this closes fd too */
208 s
->sources
[fd
] = NULL
;
215 int journal_remote_add_source(RemoteServer
*s
, int fd
, char* name
, bool own_name
) {
216 RemoteSource
*source
= NULL
;
219 /* This takes ownership of name, even on failure, if own_name is true. */
231 r
= get_source_for_fd(s
, fd
, name
, &source
);
233 log_error_errno(r
, "Failed to create source for fd:%d (%s): %m",
239 r
= sd_event_add_io(s
->events
, &source
->event
,
240 fd
, EPOLLIN
|EPOLLRDHUP
|EPOLLPRI
,
241 dispatch_raw_source_event
, source
);
243 /* Add additional source for buffer processing. It will be
245 r
= sd_event_add_defer(s
->events
, &source
->buffer_event
,
246 dispatch_raw_source_until_block
, source
);
248 sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_OFF
);
249 } else if (r
== -EPERM
) {
250 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd
, name
);
251 r
= sd_event_add_defer(s
->events
, &source
->event
,
252 dispatch_blocking_source_event
, source
);
254 sd_event_source_set_enabled(source
->event
, SD_EVENT_ON
);
257 log_error_errno(r
, "Failed to register event source for fd:%d: %m",
262 r
= sd_event_source_set_description(source
->event
, name
);
264 log_error_errno(r
, "Failed to set source name for fd:%d: %m", fd
);
268 return 1; /* work to do */
271 remove_source(s
, fd
);
275 int journal_remote_add_raw_socket(RemoteServer
*s
, int fd
) {
277 _cleanup_close_
int fd_
= fd
;
278 char name
[STRLEN("raw-socket-") + DECIMAL_STR_MAX(int) + 1];
282 r
= sd_event_add_io(s
->events
, &s
->listen_event
,
284 dispatch_raw_connection_event
, s
);
288 xsprintf(name
, "raw-socket-%d", fd
);
290 r
= sd_event_source_set_description(s
->listen_event
, name
);
299 /**********************************************************************
300 **********************************************************************
301 **********************************************************************/
303 int journal_remote_server_init(
306 JournalWriteSplitMode split_mode
,
314 assert(journal_remote_server_global
== NULL
);
315 journal_remote_server_global
= s
;
317 s
->split_mode
= split_mode
;
318 s
->compress
= compress
;
323 else if (split_mode
== JOURNAL_WRITE_SPLIT_NONE
)
324 s
->output
= REMOTE_JOURNAL_PATH
"/remote.journal";
325 else if (split_mode
== JOURNAL_WRITE_SPLIT_HOST
)
326 s
->output
= REMOTE_JOURNAL_PATH
;
328 assert_not_reached("bad split mode");
330 r
= sd_event_default(&s
->events
);
332 return log_error_errno(r
, "Failed to allocate event loop: %m");
334 r
= init_writer_hashmap(s
);
342 static void MHDDaemonWrapper_free(MHDDaemonWrapper
*d
) {
343 MHD_stop_daemon(d
->daemon
);
344 sd_event_source_unref(d
->io_event
);
345 sd_event_source_unref(d
->timer_event
);
350 void journal_remote_server_destroy(RemoteServer
*s
) {
354 hashmap_free_with_destructor(s
->daemons
, MHDDaemonWrapper_free
);
357 assert(s
->sources_size
== 0 || s
->sources
);
358 for (i
= 0; i
< s
->sources_size
; 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
) s
->sources_size
);
394 source
= s
->sources
[fd
];
395 assert(source
->importer
.fd
== fd
);
397 r
= process_source(source
, s
->compress
, s
->seal
);
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
);
436 /* No more data for now */
437 sd_event_source_set_enabled(event
, SD_EVENT_OFF
);
439 sd_event_source_unref(event
);
444 static int dispatch_raw_source_event(sd_event_source
*event
,
448 RemoteSource
*source
= userdata
;
451 assert(source
->event
);
452 assert(source
->buffer_event
);
454 r
= journal_remote_handle_raw_source(event
, fd
, EPOLLIN
, journal_remote_server_global
);
456 /* Might have more data. We need to rerun the handler
457 * until we are sure the buffer is exhausted. */
458 sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_ON
);
463 static int dispatch_blocking_source_event(sd_event_source
*event
,
465 RemoteSource
*source
= userdata
;
467 return journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
470 static int accept_connection(
476 _cleanup_close_
int fd2
= -1;
479 log_debug("Accepting new %s connection on fd:%d", type
, fd
);
480 fd2
= accept4(fd
, &addr
->sockaddr
.sa
, &addr
->size
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
482 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
485 return log_error_errno(errno
, "accept() on fd:%d failed: %m", fd
);
488 switch(socket_address_family(addr
)) {
491 _cleanup_free_
char *a
= NULL
;
494 r
= socket_address_print(addr
, &a
);
496 return log_error_errno(r
, "socket_address_print(): %m");
498 r
= socknameinfo_pretty(&addr
->sockaddr
, addr
->size
, &b
);
500 return log_error_errno(r
, "Resolving hostname failed: %m");
502 log_debug("Accepted %s %s connection from %s",
504 socket_address_family(addr
) == AF_INET
? "IP" : "IPv6",
512 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
513 "Rejected %s connection with unsupported family %d",
514 type
, socket_address_family(addr
));
518 static int dispatch_raw_connection_event(
519 sd_event_source
*event
,
524 RemoteServer
*s
= userdata
;
526 SocketAddress addr
= {
527 .size
= sizeof(union sockaddr_union
),
530 char *hostname
= NULL
;
532 fd2
= accept_connection("raw", fd
, &addr
, &hostname
);
538 return journal_remote_add_source(s
, fd2
, hostname
, true);