1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
12 #include "alloc-util.h"
14 #include "errno-util.h"
17 #include "journal-file.h"
18 #include "journal-remote-write.h"
19 #include "journal-remote.h"
20 #include "journald-native.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
= journal_file_open_reliably(filename
,
66 s
->compress
, UINT64_MAX
, s
->seal
,
71 return log_error_errno(r
, "Failed to open output journal %s: %m", filename
);
73 log_debug("Opened output file %s", w
->journal
->path
);
77 /**********************************************************************
78 **********************************************************************
79 **********************************************************************/
81 static int init_writer_hashmap(RemoteServer
*s
) {
82 static const struct hash_ops
* const hash_ops
[] = {
83 [JOURNAL_WRITE_SPLIT_NONE
] = NULL
,
84 [JOURNAL_WRITE_SPLIT_HOST
] = &string_hash_ops
,
88 assert(s
->split_mode
>= 0 && s
->split_mode
< (int) ELEMENTSOF(hash_ops
));
90 s
->writers
= hashmap_new(hash_ops
[s
->split_mode
]);
97 int journal_remote_get_writer(RemoteServer
*s
, const char *host
, Writer
**writer
) {
98 _cleanup_(writer_unrefp
) Writer
*w
= NULL
;
102 switch(s
->split_mode
) {
103 case JOURNAL_WRITE_SPLIT_NONE
:
104 key
= "one and only";
107 case JOURNAL_WRITE_SPLIT_HOST
:
113 assert_not_reached();
116 w
= hashmap_get(s
->writers
, key
);
124 if (s
->split_mode
== JOURNAL_WRITE_SPLIT_HOST
) {
125 w
->hashmap_key
= strdup(key
);
130 r
= open_output(s
, w
, host
);
134 r
= hashmap_put(s
->writers
, w
->hashmap_key
?: key
, w
);
139 *writer
= TAKE_PTR(w
);
144 /**********************************************************************
145 **********************************************************************
146 **********************************************************************/
148 /* This should go away as soon as µhttpd allows state to be passed around. */
149 RemoteServer
*journal_remote_server_global
;
151 static int dispatch_raw_source_event(sd_event_source
*event
,
155 static int dispatch_raw_source_until_block(sd_event_source
*event
,
157 static int dispatch_blocking_source_event(sd_event_source
*event
,
159 static int dispatch_raw_connection_event(sd_event_source
*event
,
164 static int get_source_for_fd(RemoteServer
*s
,
165 int fd
, char *name
, RemoteSource
**source
) {
169 /* This takes ownership of name, but only on success. */
174 if (!GREEDY_REALLOC0(s
->sources
, fd
+ 1))
177 r
= journal_remote_get_writer(s
, name
, &writer
);
179 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
182 if (!s
->sources
[fd
]) {
183 s
->sources
[fd
] = source_new(fd
, false, name
, writer
);
184 if (!s
->sources
[fd
]) {
185 writer_unref(writer
);
192 *source
= s
->sources
[fd
];
196 static int remove_source(RemoteServer
*s
, int fd
) {
197 RemoteSource
*source
;
200 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
202 source
= s
->sources
[fd
];
204 /* this closes fd too */
206 s
->sources
[fd
] = NULL
;
213 int journal_remote_add_source(RemoteServer
*s
, int fd
, char* name
, bool own_name
) {
214 RemoteSource
*source
= NULL
;
217 /* This takes ownership of name, even on failure, if own_name is true. */
229 r
= get_source_for_fd(s
, fd
, name
, &source
);
231 log_error_errno(r
, "Failed to create source for fd:%d (%s): %m",
237 r
= sd_event_add_io(s
->events
, &source
->event
,
238 fd
, EPOLLIN
|EPOLLRDHUP
|EPOLLPRI
,
239 dispatch_raw_source_event
, source
);
241 /* Add additional source for buffer processing. It will be
243 r
= sd_event_add_defer(s
->events
, &source
->buffer_event
,
244 dispatch_raw_source_until_block
, source
);
246 r
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_OFF
);
247 } else if (r
== -EPERM
) {
248 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd
, name
);
249 r
= sd_event_add_defer(s
->events
, &source
->event
,
250 dispatch_blocking_source_event
, source
);
252 r
= sd_event_source_set_enabled(source
->event
, SD_EVENT_ON
);
255 log_error_errno(r
, "Failed to register event source for fd:%d: %m",
260 r
= sd_event_source_set_description(source
->event
, name
);
262 log_error_errno(r
, "Failed to set source name for fd:%d: %m", fd
);
266 return 1; /* work to do */
269 remove_source(s
, fd
);
273 int journal_remote_add_raw_socket(RemoteServer
*s
, int fd
) {
275 _unused_ _cleanup_close_
int fd_
= fd
;
276 char name
[STRLEN("raw-socket-") + DECIMAL_STR_MAX(int) + 1];
280 r
= sd_event_add_io(s
->events
, &s
->listen_event
,
282 dispatch_raw_connection_event
, s
);
286 xsprintf(name
, "raw-socket-%d", fd
);
288 r
= sd_event_source_set_description(s
->listen_event
, name
);
297 /**********************************************************************
298 **********************************************************************
299 **********************************************************************/
301 int journal_remote_server_init(
304 JournalWriteSplitMode split_mode
,
312 assert(journal_remote_server_global
== NULL
);
313 journal_remote_server_global
= s
;
315 s
->split_mode
= split_mode
;
316 s
->compress
= compress
;
321 else if (split_mode
== JOURNAL_WRITE_SPLIT_NONE
)
322 s
->output
= REMOTE_JOURNAL_PATH
"/remote.journal";
323 else if (split_mode
== JOURNAL_WRITE_SPLIT_HOST
)
324 s
->output
= REMOTE_JOURNAL_PATH
;
326 assert_not_reached();
328 r
= sd_event_default(&s
->events
);
330 return log_error_errno(r
, "Failed to allocate event loop: %m");
332 r
= init_writer_hashmap(s
);
340 static void MHDDaemonWrapper_free(MHDDaemonWrapper
*d
) {
341 MHD_stop_daemon(d
->daemon
);
342 sd_event_source_unref(d
->io_event
);
343 sd_event_source_unref(d
->timer_event
);
348 void journal_remote_server_destroy(RemoteServer
*s
) {
352 hashmap_free_with_destructor(s
->daemons
, MHDDaemonWrapper_free
);
355 for (i
= 0; i
< MALLOC_ELEMENTSOF(s
->sources
); i
++)
359 writer_unref(s
->_single_writer
);
360 hashmap_free(s
->writers
);
362 sd_event_source_unref(s
->sigterm_event
);
363 sd_event_source_unref(s
->sigint_event
);
364 sd_event_source_unref(s
->listen_event
);
365 sd_event_unref(s
->events
);
367 if (s
== journal_remote_server_global
)
368 journal_remote_server_global
= NULL
;
370 /* fds that we're listening on remain open... */
373 /**********************************************************************
374 **********************************************************************
375 **********************************************************************/
377 int journal_remote_handle_raw_source(
378 sd_event_source
*event
,
383 RemoteSource
*source
;
386 /* Returns 1 if there might be more data pending,
387 * 0 if data is currently exhausted, negative on error.
390 assert(fd
>= 0 && fd
< (ssize_t
) MALLOC_ELEMENTSOF(s
->sources
));
391 source
= s
->sources
[fd
];
392 assert(source
->importer
.fd
== fd
);
394 r
= process_source(source
, s
->compress
, s
->seal
);
395 if (journal_importer_eof(&source
->importer
)) {
398 log_debug("EOF reached with source %s (fd=%d)",
399 source
->importer
.name
, source
->importer
.fd
);
401 remaining
= journal_importer_bytes_remaining(&source
->importer
);
403 log_notice("Premature EOF. %zu bytes lost.", remaining
);
404 remove_source(s
, source
->importer
.fd
);
405 log_debug("%zu active sources remaining", s
->active
);
407 } else if (r
== -E2BIG
) {
408 log_notice("Entry with too many fields, skipped");
410 } else if (r
== -ENOBUFS
) {
411 log_notice("Entry too big, skipped");
413 } else if (r
== -EAGAIN
) {
416 log_debug_errno(r
, "Closing connection: %m");
417 remove_source(s
, fd
);
423 static int dispatch_raw_source_until_block(sd_event_source
*event
,
425 RemoteSource
*source
= userdata
;
428 /* Make sure event stays around even if source is destroyed */
429 sd_event_source_ref(event
);
431 r
= journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
435 /* No more data for now */
436 k
= sd_event_source_set_enabled(event
, SD_EVENT_OFF
);
441 sd_event_source_unref(event
);
446 static int dispatch_raw_source_event(sd_event_source
*event
,
450 RemoteSource
*source
= userdata
;
453 assert(source
->event
);
454 assert(source
->buffer_event
);
456 r
= journal_remote_handle_raw_source(event
, fd
, EPOLLIN
, journal_remote_server_global
);
460 /* Might have more data. We need to rerun the handler
461 * until we are sure the buffer is exhausted. */
462 k
= sd_event_source_set_enabled(source
->buffer_event
, SD_EVENT_ON
);
470 static int dispatch_blocking_source_event(sd_event_source
*event
,
472 RemoteSource
*source
= userdata
;
474 return journal_remote_handle_raw_source(event
, source
->importer
.fd
, EPOLLIN
, journal_remote_server_global
);
477 static int accept_connection(
483 _cleanup_close_
int fd2
= -1;
486 log_debug("Accepting new %s connection on fd:%d", type
, fd
);
487 fd2
= accept4(fd
, &addr
->sockaddr
.sa
, &addr
->size
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
489 if (ERRNO_IS_ACCEPT_AGAIN(errno
))
492 return log_error_errno(errno
, "accept() on fd:%d failed: %m", fd
);
495 switch(socket_address_family(addr
)) {
498 _cleanup_free_
char *a
= NULL
;
501 r
= socket_address_print(addr
, &a
);
503 return log_error_errno(r
, "socket_address_print(): %m");
505 r
= socknameinfo_pretty(&addr
->sockaddr
, addr
->size
, &b
);
507 return log_error_errno(r
, "Resolving hostname failed: %m");
509 log_debug("Accepted %s %s connection from %s",
511 af_to_ipv4_ipv6(socket_address_family(addr
)),
519 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
520 "Rejected %s connection with unsupported family %d",
521 type
, socket_address_family(addr
));
525 static int dispatch_raw_connection_event(
526 sd_event_source
*event
,
531 RemoteServer
*s
= userdata
;
533 SocketAddress addr
= {
534 .size
= sizeof(union sockaddr_union
),
537 char *hostname
= NULL
;
539 fd2
= accept_connection("raw", fd
, &addr
, &hostname
);
545 return journal_remote_add_source(s
, fd2
, hostname
, true);