1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <selinux/selinux.h>
8 #include <sys/signalfd.h>
9 #include <sys/statvfs.h>
10 #include <linux/sockios.h>
12 #include "sd-daemon.h"
13 #include "sd-journal.h"
14 #include "sd-messages.h"
17 #include "alloc-util.h"
18 #include "audit-util.h"
19 #include "cgroup-util.h"
20 #include "conf-parser.h"
21 #include "creds-util.h"
22 #include "dirent-util.h"
23 #include "extract-word.h"
26 #include "format-util.h"
29 #include "hostname-util.h"
30 #include "id128-util.h"
31 #include "initrd-util.h"
32 #include "iovec-util.h"
33 #include "journal-authenticate.h"
34 #include "journal-file-util.h"
35 #include "journal-internal.h"
36 #include "journal-vacuum.h"
37 #include "journald-audit.h"
38 #include "journald-context.h"
39 #include "journald-kmsg.h"
40 #include "journald-native.h"
41 #include "journald-rate-limit.h"
42 #include "journald-server.h"
43 #include "journald-socket.h"
44 #include "journald-stream.h"
45 #include "journald-syslog.h"
47 #include "memory-util.h"
48 #include "missing_audit.h"
50 #include "parse-util.h"
51 #include "path-util.h"
52 #include "proc-cmdline.h"
53 #include "process-util.h"
55 #include "selinux-util.h"
56 #include "signal-util.h"
57 #include "socket-netlink.h"
58 #include "socket-util.h"
59 #include "stdio-util.h"
60 #include "string-table.h"
61 #include "string-util.h"
62 #include "syslog-util.h"
63 #include "uid-classification.h"
64 #include "user-util.h"
65 #include "varlink-io.systemd.Journal.h"
67 #define USER_JOURNALS_MAX 1024
69 #define DEFAULT_SYNC_INTERVAL_USEC (5*USEC_PER_MINUTE)
70 #define DEFAULT_RATE_LIMIT_INTERVAL (30*USEC_PER_SEC)
71 #define DEFAULT_RATE_LIMIT_BURST 10000
72 #define DEFAULT_MAX_FILE_USEC USEC_PER_MONTH
74 #define DEFAULT_KMSG_OWN_INTERVAL (5 * USEC_PER_SEC)
75 #define DEFAULT_KMSG_OWN_BURST 50
77 #define RECHECK_SPACE_USEC (30*USEC_PER_SEC)
79 #define NOTIFY_SNDBUF_SIZE (8*1024*1024)
81 /* The period to insert between posting changes for coalescing */
82 #define POST_CHANGE_TIMER_INTERVAL_USEC (250*USEC_PER_MSEC)
84 /* Pick a good default that is likely to fit into AF_UNIX and AF_INET SOCK_DGRAM datagrams, and even leaves some room
85 * for a bit of additional metadata. */
86 #define DEFAULT_LINE_MAX (48*1024)
88 #define DEFERRED_CLOSES_MAX (4096)
90 #define IDLE_TIMEOUT_USEC (30*USEC_PER_SEC)
92 #define FAILED_TO_WRITE_ENTRY_RATELIMIT ((const RateLimit) { .interval = 1 * USEC_PER_SEC, .burst = 1 })
94 static int server_determine_path_usage(
100 _cleanup_closedir_
DIR *d
= NULL
;
110 return log_ratelimit_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
,
111 errno
, JOURNAL_LOG_RATELIMIT
, "Failed to open %s: %m", path
);
113 if (fstatvfs(dirfd(d
), &ss
) < 0)
114 return log_ratelimit_error_errno(errno
, JOURNAL_LOG_RATELIMIT
,
115 "Failed to fstatvfs(%s): %m", path
);
117 *ret_free
= ss
.f_bsize
* ss
.f_bavail
;
119 FOREACH_DIRENT_ALL(de
, d
, break) {
122 if (!endswith(de
->d_name
, ".journal") &&
123 !endswith(de
->d_name
, ".journal~"))
126 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
127 log_debug_errno(errno
, "Failed to stat %s/%s, ignoring: %m", path
, de
->d_name
);
131 if (!S_ISREG(st
.st_mode
))
134 *ret_used
+= (uint64_t) st
.st_blocks
* 512UL;
140 static void cache_space_invalidate(JournalStorageSpace
*space
) {
144 static int cache_space_refresh(Server
*s
, JournalStorage
*storage
) {
145 JournalStorageSpace
*space
;
146 JournalMetrics
*metrics
;
147 uint64_t vfs_used
, vfs_avail
, avail
;
153 metrics
= &storage
->metrics
;
154 space
= &storage
->space
;
156 ts
= now(CLOCK_MONOTONIC
);
158 if (space
->timestamp
!= 0 && usec_add(space
->timestamp
, RECHECK_SPACE_USEC
) > ts
)
161 r
= server_determine_path_usage(s
, storage
->path
, &vfs_used
, &vfs_avail
);
165 space
->vfs_used
= vfs_used
;
166 space
->vfs_available
= vfs_avail
;
168 avail
= LESS_BY(vfs_avail
, metrics
->keep_free
);
170 space
->limit
= CLAMP(vfs_used
+ avail
, metrics
->min_use
, metrics
->max_use
);
171 space
->available
= LESS_BY(space
->limit
, vfs_used
);
172 space
->timestamp
= ts
;
176 static void patch_min_use(JournalStorage
*storage
) {
179 /* Let's bump the min_use limit to the current usage on disk. We do
180 * this when starting up and first opening the journal files. This way
181 * sudden spikes in disk usage will not cause journald to vacuum files
182 * without bounds. Note that this means that only a restart of journald
183 * will make it reset this value. */
185 storage
->metrics
.min_use
= MAX(storage
->metrics
.min_use
, storage
->space
.vfs_used
);
188 static JournalStorage
* server_current_storage(Server
*s
) {
191 return s
->system_journal
? &s
->system_storage
: &s
->runtime_storage
;
194 static int server_determine_space(Server
*s
, uint64_t *available
, uint64_t *limit
) {
200 js
= server_current_storage(s
);
202 r
= cache_space_refresh(s
, js
);
205 *available
= js
->space
.available
;
207 *limit
= js
->space
.limit
;
212 void server_space_usage_message(Server
*s
, JournalStorage
*storage
) {
216 storage
= server_current_storage(s
);
218 if (cache_space_refresh(s
, storage
) < 0)
221 const JournalMetrics
*metrics
= &storage
->metrics
;
223 server_driver_message(s
, 0,
224 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_USAGE_STR
,
225 LOG_MESSAGE("%s (%s) is %s, max %s, %s free.",
226 storage
->name
, storage
->path
,
227 FORMAT_BYTES(storage
->space
.vfs_used
),
228 FORMAT_BYTES(storage
->space
.limit
),
229 FORMAT_BYTES(storage
->space
.available
)),
230 "JOURNAL_NAME=%s", storage
->name
,
231 "JOURNAL_PATH=%s", storage
->path
,
232 "CURRENT_USE=%"PRIu64
, storage
->space
.vfs_used
,
233 "CURRENT_USE_PRETTY=%s", FORMAT_BYTES(storage
->space
.vfs_used
),
234 "MAX_USE=%"PRIu64
, metrics
->max_use
,
235 "MAX_USE_PRETTY=%s", FORMAT_BYTES(metrics
->max_use
),
236 "DISK_KEEP_FREE=%"PRIu64
, metrics
->keep_free
,
237 "DISK_KEEP_FREE_PRETTY=%s", FORMAT_BYTES(metrics
->keep_free
),
238 "DISK_AVAILABLE=%"PRIu64
, storage
->space
.vfs_available
,
239 "DISK_AVAILABLE_PRETTY=%s", FORMAT_BYTES(storage
->space
.vfs_available
),
240 "LIMIT=%"PRIu64
, storage
->space
.limit
,
241 "LIMIT_PRETTY=%s", FORMAT_BYTES(storage
->space
.limit
),
242 "AVAILABLE=%"PRIu64
, storage
->space
.available
,
243 "AVAILABLE_PRETTY=%s", FORMAT_BYTES(storage
->space
.available
),
247 static void server_add_acls(JournalFile
*f
, uid_t uid
) {
253 if (uid_for_system_journal(uid
))
256 r
= fd_add_uid_acl_permission(f
->fd
, uid
, ACL_READ
);
258 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
259 "Failed to set ACL on %s, ignoring: %m", f
->path
);
263 static int server_open_journal(
269 JournalMetrics
*metrics
,
272 _cleanup_(journal_file_offline_closep
) JournalFile
*f
= NULL
;
273 JournalFileFlags file_flags
;
281 (s
->compress
.enabled
? JOURNAL_COMPRESS
: 0) |
282 (seal
? JOURNAL_SEAL
: 0) |
283 JOURNAL_STRICT_ORDER
;
285 set_clear_with_destructor(s
->deferred_closes
, journal_file_offline_close
);
288 r
= journal_file_open_reliably(
293 s
->compress
.threshold_bytes
,
298 r
= journal_file_open(
304 s
->compress
.threshold_bytes
,
307 /* template= */ NULL
,
312 r
= journal_file_enable_post_change_timer(f
, s
->event
, POST_CHANGE_TIMER_INTERVAL_USEC
);
320 static bool server_flushed_flag_is_set(Server
*s
) {
325 /* We don't support the "flushing" concept for namespace instances, we assume them to always have
330 fn
= strjoina(s
->runtime_directory
, "/flushed");
331 return access(fn
, F_OK
) >= 0;
334 static int server_system_journal_open(
336 bool flush_requested
,
337 bool relinquish_requested
) {
342 if (!s
->system_journal
&&
343 IN_SET(s
->storage
, STORAGE_PERSISTENT
, STORAGE_AUTO
) &&
344 (flush_requested
|| server_flushed_flag_is_set(s
)) &&
345 !relinquish_requested
) {
347 /* If in auto mode: first try to create the machine path, but not the prefix.
349 * If in persistent mode: create /var/log/journal and the machine path */
351 if (s
->storage
== STORAGE_PERSISTENT
)
352 (void) mkdir_parents(s
->system_storage
.path
, 0755);
354 (void) mkdir(s
->system_storage
.path
, 0755);
356 fn
= strjoina(s
->system_storage
.path
, "/system.journal");
357 r
= server_open_journal(
359 /* reliably= */ true,
363 &s
->system_storage
.metrics
,
366 server_add_acls(s
->system_journal
, 0);
367 (void) cache_space_refresh(s
, &s
->system_storage
);
368 patch_min_use(&s
->system_storage
);
370 if (!IN_SET(r
, -ENOENT
, -EROFS
))
371 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
372 "Failed to open system journal: %m");
377 /* If the runtime journal is open, and we're post-flush, we're recovering from a failed
378 * system journal rotate (ENOSPC) for which the runtime journal was reopened.
380 * Perform an implicit flush to var, leaving the runtime journal closed, now that the system
383 if (!flush_requested
)
384 (void) server_flush_to_var(s
, true);
387 if (!s
->runtime_journal
&&
388 (s
->storage
!= STORAGE_NONE
)) {
390 fn
= strjoina(s
->runtime_storage
.path
, "/system.journal");
392 if (!s
->system_journal
|| relinquish_requested
) {
394 /* OK, we really need the runtime journal, so create it if necessary. */
396 (void) mkdir_parents(s
->runtime_storage
.path
, 0755);
397 (void) mkdir(s
->runtime_storage
.path
, 0750);
399 r
= server_open_journal(
401 /* reliably= */ true,
405 &s
->runtime_storage
.metrics
,
406 &s
->runtime_journal
);
408 return log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
409 "Failed to open runtime journal: %m");
411 } else if (!server_flushed_flag_is_set(s
)) {
412 /* Try to open the runtime journal, but only if it already exists, so that we can
413 * flush it into the system journal */
415 r
= server_open_journal(
417 /* reliably= */ false,
421 &s
->runtime_storage
.metrics
,
422 &s
->runtime_journal
);
425 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
426 "Failed to open runtime journal: %m");
432 if (s
->runtime_journal
) {
433 server_add_acls(s
->runtime_journal
, 0);
434 (void) cache_space_refresh(s
, &s
->runtime_storage
);
435 patch_min_use(&s
->runtime_storage
);
442 static int server_find_user_journal(Server
*s
, uid_t uid
, JournalFile
**ret
) {
443 _cleanup_(journal_file_offline_closep
) JournalFile
*f
= NULL
;
444 _cleanup_free_
char *p
= NULL
;
447 assert(!uid_for_system_journal(uid
));
449 f
= ordered_hashmap_get(s
->user_journals
, UID_TO_PTR(uid
));
453 if (asprintf(&p
, "%s/user-" UID_FMT
".journal", s
->system_storage
.path
, uid
) < 0)
456 /* Too many open? Then let's close one (or more) */
457 while (ordered_hashmap_size(s
->user_journals
) >= USER_JOURNALS_MAX
) {
460 assert_se(first
= ordered_hashmap_steal_first(s
->user_journals
));
461 (void) journal_file_offline_close(first
);
464 r
= server_open_journal(
466 /* reliably= */ true,
470 &s
->system_storage
.metrics
,
475 r
= ordered_hashmap_put(s
->user_journals
, UID_TO_PTR(uid
), f
);
479 server_add_acls(f
, uid
);
486 static JournalFile
* server_find_journal(Server
*s
, uid_t uid
) {
491 /* A rotate that fails to create the new journal (ENOSPC) leaves the rotated journal as NULL. Unless
492 * we revisit opening, even after space is made available we'll continue to return NULL indefinitely.
494 * system_journal_open() is a noop if the journals are already open, so we can just call it here to
495 * recover from failed rotates (or anything else that's left the journals as NULL).
497 * Fixes https://github.com/systemd/systemd/issues/3968 */
498 (void) server_system_journal_open(s
, /* flush_requested= */ false, /* relinquish_requested= */ false);
500 /* We split up user logs only on /var, not on /run. If the runtime file is open, we write to it
501 * exclusively, in order to guarantee proper order as soon as we flush /run to /var and close the
504 if (s
->runtime_journal
)
505 return s
->runtime_journal
;
507 /* If we are not in persistent mode, then we need return NULL immediately rather than opening a
508 * persistent journal of any sort.
510 * Fixes https://github.com/systemd/systemd/issues/20390 */
511 if (!IN_SET(s
->storage
, STORAGE_AUTO
, STORAGE_PERSISTENT
))
514 if (!uid_for_system_journal(uid
)) {
515 JournalFile
*f
= NULL
;
517 r
= server_find_user_journal(s
, uid
, &f
);
519 return ASSERT_PTR(f
);
521 log_warning_errno(r
, "Failed to open user journal file, falling back to system journal: %m");
524 return s
->system_journal
;
527 static int server_do_rotate(
534 JournalFileFlags file_flags
;
543 (s
->compress
.enabled
? JOURNAL_COMPRESS
: 0)|
544 (seal
? JOURNAL_SEAL
: 0) |
545 JOURNAL_STRICT_ORDER
;
547 r
= journal_file_rotate(f
, s
->mmap
, file_flags
, s
->compress
.threshold_bytes
, s
->deferred_closes
);
550 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
551 "Failed to rotate %s: %m", (*f
)->path
);
553 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
554 "Failed to create new %s journal: %m", name
);
557 server_add_acls(*f
, uid
);
561 static void server_process_deferred_closes(Server
*s
) {
564 /* Perform any deferred closes which aren't still offlining. */
565 SET_FOREACH(f
, s
->deferred_closes
) {
566 if (journal_file_is_offlining(f
))
569 (void) set_remove(s
->deferred_closes
, f
);
570 (void) journal_file_offline_close(f
);
574 static void server_vacuum_deferred_closes(Server
*s
) {
577 /* Make some room in the deferred closes list, so that it doesn't grow without bounds */
578 if (set_size(s
->deferred_closes
) < DEFERRED_CLOSES_MAX
)
581 /* Let's first remove all journal files that might already have completed closing */
582 server_process_deferred_closes(s
);
584 /* And now, let's close some more until we reach the limit again. */
585 while (set_size(s
->deferred_closes
) >= DEFERRED_CLOSES_MAX
) {
588 assert_se(f
= set_steal_first(s
->deferred_closes
));
589 journal_file_offline_close(f
);
593 static int server_archive_offline_user_journals(Server
*s
) {
594 _cleanup_closedir_
DIR *d
= NULL
;
599 d
= opendir(s
->system_storage
.path
);
604 return log_ratelimit_error_errno(errno
, JOURNAL_LOG_RATELIMIT
,
605 "Failed to open %s: %m", s
->system_storage
.path
);
609 _cleanup_free_
char *full
= NULL
;
610 _cleanup_close_
int fd
= -EBADF
;
616 de
= readdir_no_dot(d
);
619 log_ratelimit_warning_errno(errno
, JOURNAL_LOG_RATELIMIT
,
620 "Failed to enumerate %s, ignoring: %m",
621 s
->system_storage
.path
);
625 r
= journal_file_parse_uid_from_filename(de
->d_name
, &uid
);
627 /* Don't warn if the file is not an online or offline user journal. */
629 log_warning_errno(r
, "Failed to parse UID from file name '%s', ignoring: %m", de
->d_name
);
633 /* Already rotated in the above loop? i.e. is it an open user journal? */
634 if (ordered_hashmap_contains(s
->user_journals
, UID_TO_PTR(uid
)))
637 full
= path_join(s
->system_storage
.path
, de
->d_name
);
641 fd
= openat(dirfd(d
), de
->d_name
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
|O_NONBLOCK
);
643 log_ratelimit_full_errno(IN_SET(errno
, ELOOP
, ENOENT
) ? LOG_DEBUG
: LOG_WARNING
,
644 errno
, JOURNAL_LOG_RATELIMIT
,
645 "Failed to open journal file '%s' for rotation: %m", full
);
649 /* Make some room in the set of deferred close()s */
650 server_vacuum_deferred_closes(s
);
652 /* Open the file briefly, so that we can archive it */
653 r
= journal_file_open(
657 (s
->compress
.enabled
? JOURNAL_COMPRESS
: 0) |
658 (s
->seal
? JOURNAL_SEAL
: 0), /* strict order does not matter here */
660 s
->compress
.threshold_bytes
,
661 &s
->system_storage
.metrics
,
663 /* template= */ NULL
,
666 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
667 "Failed to read journal file %s for rotation, trying to move it out of the way: %m",
670 r
= journal_file_dispose(dirfd(d
), de
->d_name
);
672 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
673 "Failed to move %s out of the way, ignoring: %m",
676 log_debug("Successfully moved %s out of the way.", full
);
681 TAKE_FD(fd
); /* Donated to journal_file_open() */
683 journal_file_write_final_tag(f
);
684 r
= journal_file_archive(f
, NULL
);
686 log_debug_errno(r
, "Failed to archive journal file '%s', ignoring: %m", full
);
688 journal_file_initiate_close(TAKE_PTR(f
), s
->deferred_closes
);
694 void server_rotate(Server
*s
) {
699 log_debug("Rotating...");
701 /* First, rotate the system journal (either in its runtime flavour or in its runtime flavour) */
702 (void) server_do_rotate(s
, &s
->runtime_journal
, "runtime", /* seal= */ false, /* uid= */ 0);
703 (void) server_do_rotate(s
, &s
->system_journal
, "system", s
->seal
, /* uid= */ 0);
705 /* Then, rotate all user journals we have open (keeping them open) */
706 ORDERED_HASHMAP_FOREACH_KEY(f
, k
, s
->user_journals
) {
707 r
= server_do_rotate(s
, &f
, "user", s
->seal
, PTR_TO_UID(k
));
709 ordered_hashmap_replace(s
->user_journals
, k
, f
);
711 /* Old file has been closed and deallocated */
712 ordered_hashmap_remove(s
->user_journals
, k
);
715 /* Finally, also rotate all user journals we currently do not have open. (But do so only if we
716 * actually have access to /var, i.e. are not in the log-to-runtime-journal mode). */
717 if (!s
->runtime_journal
)
718 (void) server_archive_offline_user_journals(s
);
720 server_process_deferred_closes(s
);
723 void server_sync(Server
*s
) {
727 if (s
->system_journal
) {
728 r
= journal_file_set_offline(s
->system_journal
, false);
730 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
731 "Failed to sync system journal, ignoring: %m");
734 ORDERED_HASHMAP_FOREACH(f
, s
->user_journals
) {
735 r
= journal_file_set_offline(f
, false);
737 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
738 "Failed to sync user journal, ignoring: %m");
741 if (s
->sync_event_source
) {
742 r
= sd_event_source_set_enabled(s
->sync_event_source
, SD_EVENT_OFF
);
744 log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
745 "Failed to disable sync timer source: %m");
748 s
->sync_scheduled
= false;
751 static void server_do_vacuum(Server
*s
, JournalStorage
*storage
, bool verbose
) {
758 (void) cache_space_refresh(s
, storage
);
761 server_space_usage_message(s
, storage
);
763 r
= journal_directory_vacuum(storage
->path
, storage
->space
.limit
,
764 storage
->metrics
.n_max_files
, s
->max_retention_usec
,
765 &s
->oldest_file_usec
, verbose
);
766 if (r
< 0 && r
!= -ENOENT
)
767 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
768 "Failed to vacuum %s, ignoring: %m", storage
->path
);
770 cache_space_invalidate(&storage
->space
);
773 void server_vacuum(Server
*s
, bool verbose
) {
776 log_debug("Vacuuming...");
778 s
->oldest_file_usec
= 0;
780 if (s
->system_journal
)
781 server_do_vacuum(s
, &s
->system_storage
, verbose
);
782 if (s
->runtime_journal
)
783 server_do_vacuum(s
, &s
->runtime_storage
, verbose
);
786 static void server_cache_machine_id(Server
*s
) {
792 r
= sd_id128_get_machine(&id
);
796 sd_id128_to_string(id
, stpcpy(s
->machine_id_field
, "_MACHINE_ID="));
799 static void server_cache_boot_id(Server
*s
) {
805 r
= sd_id128_get_boot(&id
);
809 sd_id128_to_string(id
, stpcpy(s
->boot_id_field
, "_BOOT_ID="));
812 static void server_cache_hostname(Server
*s
) {
813 _cleanup_free_
char *t
= NULL
;
818 t
= gethostname_malloc();
822 x
= strjoin("_HOSTNAME=", t
);
826 free_and_replace(s
->hostname_field
, x
);
829 static bool shall_try_append_again(JournalFile
*f
, int r
) {
832 case -E2BIG
: /* Hit configured limit */
833 case -EFBIG
: /* Hit fs limit */
834 case -EDQUOT
: /* Quota limit hit */
835 case -ENOSPC
: /* Disk full */
836 log_debug_errno(r
, "%s: Allocation limit reached, rotating.", f
->path
);
839 case -EROFS
: /* Read-only file system */
840 /* When appending an entry fails if shall_try_append_again returns true, the journal is
841 * rotated. If the FS is read-only, rotation will fail and s->system_journal will be set to
842 * NULL. After that, when find_journal will try to open the journal since s->system_journal
843 * will be NULL, it will open the runtime journal. */
844 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Read-only file system, rotating.", f
->path
);
847 case -EIO
: /* I/O error of some kind (mmap) */
848 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: IO error, rotating.", f
->path
);
851 case -EHOSTDOWN
: /* Other machine */
852 log_ratelimit_info_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Journal file from other machine, rotating.", f
->path
);
855 case -EBUSY
: /* Unclean shutdown */
856 log_ratelimit_info_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Unclean shutdown, rotating.", f
->path
);
859 case -EPROTONOSUPPORT
: /* Unsupported feature */
860 log_ratelimit_info_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Unsupported feature, rotating.", f
->path
);
863 case -EBADMSG
: /* Corrupted */
864 case -ENODATA
: /* Truncated */
865 case -ESHUTDOWN
: /* Already archived */
866 case -EADDRNOTAVAIL
: /* Referenced object offset out of bounds */
867 log_ratelimit_info_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Journal file corrupted, rotating.", f
->path
);
870 case -EIDRM
: /* Journal file has been deleted */
871 log_ratelimit_info_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Journal file has been deleted, rotating.", f
->path
);
874 case -EREMCHG
: /* Wallclock time (CLOCK_REALTIME) jumped backwards relative to last journal entry */
875 log_ratelimit_info_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Realtime clock jumped backwards relative to last journal entry, rotating.", f
->path
);
878 case -ENOTNAM
: /* Monotonic time (CLOCK_MONOTONIC) jumped backwards relative to last journal entry with the same boot ID */
879 log_ratelimit_info_errno(
881 JOURNAL_LOG_RATELIMIT
,
882 "%s: Monotonic clock jumped backwards relative to last journal entry with the same boot ID, rotating.",
886 case -EILSEQ
: /* seqnum ID last used in the file doesn't match the one we'd passed when writing an entry to it */
887 log_ratelimit_info_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Journal file uses a different sequence number ID, rotating.", f
->path
);
891 log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Underlying file system does not support memory mapping or another required file system feature.", f
->path
);
895 log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
, "%s: Unexpected error while writing to journal file: %m", f
->path
);
900 static void server_write_to_journal(
903 const struct iovec
*iovec
,
907 bool vacuumed
= false, rotate
= false;
908 struct dual_timestamp ts
;
916 /* Get the closest, linearized time we have for this log event from the event loop. (Note that we do not use
917 * the source time, and not even the time the event was originally seen, but instead simply the time we started
918 * processing it, as we want strictly linear ordering in what we write out.) */
919 assert_se(sd_event_now(s
->event
, CLOCK_REALTIME
, &ts
.realtime
) >= 0);
920 assert_se(sd_event_now(s
->event
, CLOCK_MONOTONIC
, &ts
.monotonic
) >= 0);
922 if (ts
.realtime
< s
->last_realtime_clock
) {
923 /* When the time jumps backwards, let's immediately rotate. Of course, this should not happen during
924 * regular operation. However, when it does happen, then we should make sure that we start fresh files
925 * to ensure that the entries in the journal files are strictly ordered by time, in order to ensure
926 * bisection works correctly. */
928 log_ratelimit_info(JOURNAL_LOG_RATELIMIT
, "Time jumped backwards, rotating.");
932 f
= server_find_journal(s
, uid
);
936 if (journal_file_rotate_suggested(f
, s
->max_file_usec
, LOG_DEBUG
)) {
937 log_debug("%s: Journal header limits reached or header out-of-date, rotating.",
945 server_vacuum(s
, false);
948 f
= server_find_journal(s
, uid
);
953 s
->last_realtime_clock
= ts
.realtime
;
955 r
= journal_file_append_entry(
962 /* ret_object= */ NULL
,
963 /* ret_offset= */ NULL
);
965 server_schedule_sync(s
, priority
);
969 log_debug_errno(r
, "Failed to write entry to %s (%zu items, %zu bytes): %m", f
->path
, n
, iovec_total_size(iovec
, n
));
971 if (!shall_try_append_again(f
, r
))
974 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
975 "Suppressing rotation, as we already rotated immediately before write attempt. Giving up.");
980 server_vacuum(s
, false);
982 f
= server_find_journal(s
, uid
);
986 log_debug_errno(r
, "Retrying write.");
987 r
= journal_file_append_entry(
994 /* ret_object= */ NULL
,
995 /* ret_offset= */ NULL
);
997 log_ratelimit_error_errno(r
, FAILED_TO_WRITE_ENTRY_RATELIMIT
,
998 "Failed to write entry to %s (%zu items, %zu bytes) despite vacuuming, ignoring: %m",
999 f
->path
, n
, iovec_total_size(iovec
, n
));
1001 server_schedule_sync(s
, priority
);
1004 #define IOVEC_ADD_NUMERIC_FIELD(iovec, n, value, type, isset, format, field) \
1005 if (isset(value)) { \
1007 k = newa(char, STRLEN(field "=") + DECIMAL_STR_MAX(type) + 1); \
1008 sprintf(k, field "=" format, value); \
1009 iovec[n++] = IOVEC_MAKE_STRING(k); \
1012 #define IOVEC_ADD_STRING_FIELD(iovec, n, value, field) \
1013 if (!isempty(value)) { \
1015 k = strjoina(field "=", value); \
1016 iovec[n++] = IOVEC_MAKE_STRING(k); \
1019 #define IOVEC_ADD_ID128_FIELD(iovec, n, value, field) \
1020 if (!sd_id128_is_null(value)) { \
1022 k = newa(char, STRLEN(field "=") + SD_ID128_STRING_MAX); \
1023 sd_id128_to_string(value, stpcpy(k, field "=")); \
1024 iovec[n++] = IOVEC_MAKE_STRING(k); \
1027 #define IOVEC_ADD_SIZED_FIELD(iovec, n, value, value_size, field) \
1028 if (value_size > 0) { \
1030 k = newa(char, STRLEN(field "=") + value_size + 1); \
1031 *((char*) mempcpy(stpcpy(k, field "="), value, value_size)) = 0; \
1032 iovec[n++] = IOVEC_MAKE_STRING(k); \
1035 static void server_dispatch_message_real(
1037 struct iovec
*iovec
, size_t n
, size_t m
,
1038 const ClientContext
*c
,
1039 const struct timeval
*tv
,
1043 char source_time
[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t
)];
1044 _unused_ _cleanup_free_
char *cmdline1
= NULL
, *cmdline2
= NULL
;
1052 N_IOVEC_META_FIELDS
+
1053 (pid_is_valid(object_pid
) ? N_IOVEC_OBJECT_FIELDS
: 0) +
1054 client_context_extra_fields_n_iovec(c
) <= m
);
1057 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, c
->pid
, pid_t
, pid_is_valid
, PID_FMT
, "_PID");
1058 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, c
->uid
, uid_t
, uid_is_valid
, UID_FMT
, "_UID");
1059 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, c
->gid
, gid_t
, gid_is_valid
, GID_FMT
, "_GID");
1061 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->comm
, "_COMM"); /* At most TASK_COMM_LENGTH (16 bytes) */
1062 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->exe
, "_EXE"); /* A path, so at most PATH_MAX (4096 bytes) */
1065 /* At most _SC_ARG_MAX (2MB usually), which is too much to put on stack.
1066 * Let's use a heap allocation for this one. */
1067 cmdline1
= set_iovec_string_field(iovec
, &n
, "_CMDLINE=", c
->cmdline
);
1069 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->capeff
, "_CAP_EFFECTIVE"); /* Read from /proc/.../status */
1070 IOVEC_ADD_SIZED_FIELD(iovec
, n
, c
->label
, c
->label_size
, "_SELINUX_CONTEXT");
1071 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, c
->auditid
, uint32_t, audit_session_is_valid
, "%" PRIu32
, "_AUDIT_SESSION");
1072 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, c
->loginuid
, uid_t
, uid_is_valid
, UID_FMT
, "_AUDIT_LOGINUID");
1074 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->cgroup
, "_SYSTEMD_CGROUP"); /* A path */
1075 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->session
, "_SYSTEMD_SESSION");
1076 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, c
->owner_uid
, uid_t
, uid_is_valid
, UID_FMT
, "_SYSTEMD_OWNER_UID");
1077 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->unit
, "_SYSTEMD_UNIT"); /* Unit names are bounded by UNIT_NAME_MAX */
1078 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->user_unit
, "_SYSTEMD_USER_UNIT");
1079 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->slice
, "_SYSTEMD_SLICE");
1080 IOVEC_ADD_STRING_FIELD(iovec
, n
, c
->user_slice
, "_SYSTEMD_USER_SLICE");
1082 IOVEC_ADD_ID128_FIELD(iovec
, n
, c
->invocation_id
, "_SYSTEMD_INVOCATION_ID");
1084 if (c
->extra_fields_n_iovec
> 0) {
1085 memcpy(iovec
+ n
, c
->extra_fields_iovec
, c
->extra_fields_n_iovec
* sizeof(struct iovec
));
1086 n
+= c
->extra_fields_n_iovec
;
1092 if (pid_is_valid(object_pid
) && client_context_get(s
, object_pid
, NULL
, NULL
, 0, NULL
, &o
) >= 0) {
1094 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, o
->pid
, pid_t
, pid_is_valid
, PID_FMT
, "OBJECT_PID");
1095 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, o
->uid
, uid_t
, uid_is_valid
, UID_FMT
, "OBJECT_UID");
1096 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, o
->gid
, gid_t
, gid_is_valid
, GID_FMT
, "OBJECT_GID");
1098 /* See above for size limits, only ->cmdline may be large, so use a heap allocation for it. */
1099 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->comm
, "OBJECT_COMM");
1100 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->exe
, "OBJECT_EXE");
1102 cmdline2
= set_iovec_string_field(iovec
, &n
, "OBJECT_CMDLINE=", o
->cmdline
);
1104 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->capeff
, "OBJECT_CAP_EFFECTIVE");
1105 IOVEC_ADD_SIZED_FIELD(iovec
, n
, o
->label
, o
->label_size
, "OBJECT_SELINUX_CONTEXT");
1106 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, o
->auditid
, uint32_t, audit_session_is_valid
, "%" PRIu32
, "OBJECT_AUDIT_SESSION");
1107 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, o
->loginuid
, uid_t
, uid_is_valid
, UID_FMT
, "OBJECT_AUDIT_LOGINUID");
1109 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->cgroup
, "OBJECT_SYSTEMD_CGROUP");
1110 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->session
, "OBJECT_SYSTEMD_SESSION");
1111 IOVEC_ADD_NUMERIC_FIELD(iovec
, n
, o
->owner_uid
, uid_t
, uid_is_valid
, UID_FMT
, "OBJECT_SYSTEMD_OWNER_UID");
1112 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->unit
, "OBJECT_SYSTEMD_UNIT");
1113 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->user_unit
, "OBJECT_SYSTEMD_USER_UNIT");
1114 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->slice
, "OBJECT_SYSTEMD_SLICE");
1115 IOVEC_ADD_STRING_FIELD(iovec
, n
, o
->user_slice
, "OBJECT_SYSTEMD_USER_SLICE");
1117 IOVEC_ADD_ID128_FIELD(iovec
, n
, o
->invocation_id
, "OBJECT_SYSTEMD_INVOCATION_ID=");
1123 sprintf(source_time
, "_SOURCE_REALTIME_TIMESTAMP=" USEC_FMT
, timeval_load(tv
));
1124 iovec
[n
++] = IOVEC_MAKE_STRING(source_time
);
1127 /* Note that strictly speaking storing the boot id here is
1128 * redundant since the entry includes this in-line
1129 * anyway. However, we need this indexed, too. */
1130 if (!isempty(s
->boot_id_field
))
1131 iovec
[n
++] = IOVEC_MAKE_STRING(s
->boot_id_field
);
1133 if (!isempty(s
->machine_id_field
))
1134 iovec
[n
++] = IOVEC_MAKE_STRING(s
->machine_id_field
);
1136 if (!isempty(s
->hostname_field
))
1137 iovec
[n
++] = IOVEC_MAKE_STRING(s
->hostname_field
);
1139 if (!isempty(s
->namespace_field
))
1140 iovec
[n
++] = IOVEC_MAKE_STRING(s
->namespace_field
);
1142 iovec
[n
++] = in_initrd() ? IOVEC_MAKE_STRING("_RUNTIME_SCOPE=initrd") : IOVEC_MAKE_STRING("_RUNTIME_SCOPE=system");
1145 if (s
->split_mode
== SPLIT_UID
&& c
&& uid_is_valid(c
->uid
))
1146 /* Split up strictly by (non-root) UID */
1147 journal_uid
= c
->uid
;
1148 else if (s
->split_mode
== SPLIT_LOGIN
&& c
&& c
->uid
> 0 && uid_is_valid(c
->owner_uid
))
1149 /* Split up by login UIDs. We do this only if the
1150 * realuid is not root, in order not to accidentally
1151 * leak privileged information to the user that is
1152 * logged by a privileged process that is part of an
1153 * unprivileged session. */
1154 journal_uid
= c
->owner_uid
;
1158 (void) server_forward_socket(s
, iovec
, n
, priority
);
1160 server_write_to_journal(s
, journal_uid
, iovec
, n
, priority
);
1163 void server_driver_message(Server
*s
, pid_t object_pid
, const char *message_id
, const char *format
, ...) {
1165 struct iovec
*iovec
;
1173 m
= N_IOVEC_META_FIELDS
+ 5 + N_IOVEC_PAYLOAD_FIELDS
+ client_context_extra_fields_n_iovec(s
->my_context
) + N_IOVEC_OBJECT_FIELDS
;
1174 iovec
= newa(struct iovec
, m
);
1176 assert_cc(3 == LOG_FAC(LOG_DAEMON
));
1177 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_FACILITY=3");
1178 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=systemd-journald");
1180 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=driver");
1181 assert_cc(6 == LOG_INFO
);
1182 iovec
[n
++] = IOVEC_MAKE_STRING("PRIORITY=6");
1185 iovec
[n
++] = IOVEC_MAKE_STRING(message_id
);
1188 va_start(ap
, format
);
1189 r
= log_format_iovec(iovec
, m
, &n
, false, 0, format
, ap
);
1190 /* Error handling below */
1194 server_dispatch_message_real(s
, iovec
, n
, m
, s
->my_context
, /* tv= */ NULL
, LOG_INFO
, object_pid
);
1197 free(iovec
[k
++].iov_base
);
1200 /* We failed to format the message. Emit a warning instead. */
1204 xsprintf(buf
, "MESSAGE=Entry printing failed: %m");
1207 iovec
[n
++] = IOVEC_MAKE_STRING("PRIORITY=4");
1208 iovec
[n
++] = IOVEC_MAKE_STRING(buf
);
1209 server_dispatch_message_real(s
, iovec
, n
, m
, s
->my_context
, /* tv= */ NULL
, LOG_INFO
, object_pid
);
1213 void server_dispatch_message(
1215 struct iovec
*iovec
, size_t n
, size_t m
,
1217 const struct timeval
*tv
,
1221 uint64_t available
= 0;
1225 assert(iovec
|| n
== 0);
1230 if (LOG_PRI(priority
) > s
->max_level_store
)
1233 /* Stop early in case the information will not be stored
1235 if (s
->storage
== STORAGE_NONE
)
1239 (void) server_determine_space(s
, &available
, /* limit= */ NULL
);
1241 rl
= journal_ratelimit_test(s
->ratelimit
, c
->unit
, c
->log_ratelimit_interval
, c
->log_ratelimit_burst
, priority
& LOG_PRIMASK
, available
);
1245 /* Write a suppression message if we suppressed something */
1247 server_driver_message(s
, c
->pid
,
1248 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_DROPPED_STR
,
1249 LOG_MESSAGE("Suppressed %i messages from %s", rl
- 1, c
->unit
),
1250 "N_DROPPED=%i", rl
- 1,
1254 server_dispatch_message_real(s
, iovec
, n
, m
, c
, tv
, priority
, object_pid
);
1257 int server_flush_to_var(Server
*s
, bool require_flag_file
) {
1258 sd_journal
*j
= NULL
;
1266 if (!IN_SET(s
->storage
, STORAGE_AUTO
, STORAGE_PERSISTENT
))
1269 if (s
->namespace) /* Flushing concept does not exist for namespace instances */
1272 if (!s
->runtime_journal
) /* Nothing to flush? */
1275 if (require_flag_file
&& !server_flushed_flag_is_set(s
))
1278 (void) server_system_journal_open(s
, /* flush_requested=*/ true, /* relinquish_requested= */ false);
1280 if (!s
->system_journal
)
1283 /* Reset current seqnum data to avoid unnecessary rotation when switching to system journal.
1284 * See issue #30092. */
1287 log_debug("Flushing to %s...", s
->system_storage
.path
);
1289 start
= now(CLOCK_MONOTONIC
);
1291 r
= sd_journal_open(&j
, SD_JOURNAL_RUNTIME_ONLY
);
1293 return log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
,
1294 "Failed to read runtime journal: %m");
1296 sd_journal_set_data_threshold(j
, 0);
1298 SD_JOURNAL_FOREACH(j
) {
1302 f
= j
->current_file
;
1303 assert(f
&& f
->current_offset
> 0);
1307 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
1309 log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
, "Can't read entry: %m");
1313 r
= journal_file_copy_entry(
1323 if (!shall_try_append_again(s
->system_journal
, r
)) {
1324 log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
, "Can't write entry: %m");
1328 log_ratelimit_info(JOURNAL_LOG_RATELIMIT
, "Rotating system journal.");
1331 server_vacuum(s
, false);
1333 if (!s
->system_journal
) {
1334 log_ratelimit_notice(JOURNAL_LOG_RATELIMIT
,
1335 "Didn't flush runtime journal since rotation of system journal wasn't successful.");
1340 log_debug("Retrying write.");
1341 r
= journal_file_copy_entry(
1349 log_ratelimit_error_errno(r
, JOURNAL_LOG_RATELIMIT
, "Can't write entry: %m");
1357 if (s
->system_journal
)
1358 journal_file_post_change(s
->system_journal
);
1360 /* Save parent directories of runtime journals before closing runtime journals. */
1361 _cleanup_strv_free_
char **dirs
= NULL
;
1362 (void) journal_get_directories(j
, &dirs
);
1364 /* First, close all runtime journals opened in the above. */
1365 sd_journal_close(j
);
1367 /* Offline and close the 'main' runtime journal file. */
1368 s
->runtime_journal
= journal_file_offline_close(s
->runtime_journal
);
1370 /* Remove the runtime directory if the all entries are successfully flushed to /var/. */
1372 (void) rm_rf(s
->runtime_storage
.path
, REMOVE_ROOT
);
1374 /* The initrd may have a different machine ID from the host's one. Typically, that happens
1375 * when our tests running on qemu, as the host's initrd is picked as is without updating
1376 * the machine ID in the initrd with the one used in the image. Even in such the case, the
1377 * runtime journals in the subdirectory named with the initrd's machine ID are flushed to
1378 * the persistent journal. To make not the runtime journal flushed multiple times, let's
1379 * also remove the runtime directories. */
1380 STRV_FOREACH(p
, dirs
)
1381 (void) rm_rf(*p
, REMOVE_ROOT
);
1384 server_driver_message(s
, 0, NULL
,
1385 LOG_MESSAGE("Time spent on flushing to %s is %s for %u entries.",
1386 s
->system_storage
.path
,
1387 FORMAT_TIMESPAN(usec_sub_unsigned(now(CLOCK_MONOTONIC
), start
), 0),
1391 fn
= strjoina(s
->runtime_directory
, "/flushed");
1394 log_ratelimit_warning_errno(k
, JOURNAL_LOG_RATELIMIT
,
1395 "Failed to touch %s, ignoring: %m", fn
);
1397 server_refresh_idle_timer(s
);
1401 static int server_relinquish_var(Server
*s
) {
1405 if (s
->storage
== STORAGE_NONE
)
1408 if (s
->namespace) /* Concept does not exist for namespaced instances */
1411 if (s
->runtime_journal
&& !s
->system_journal
)
1414 log_debug("Relinquishing %s...", s
->system_storage
.path
);
1416 (void) server_system_journal_open(s
, /* flush_requested */ false, /* relinquish_requested=*/ true);
1418 s
->system_journal
= journal_file_offline_close(s
->system_journal
);
1419 ordered_hashmap_clear_with_destructor(s
->user_journals
, journal_file_offline_close
);
1420 set_clear_with_destructor(s
->deferred_closes
, journal_file_offline_close
);
1422 fn
= strjoina(s
->runtime_directory
, "/flushed");
1423 if (unlink(fn
) < 0 && errno
!= ENOENT
)
1424 log_ratelimit_warning_errno(errno
, JOURNAL_LOG_RATELIMIT
,
1425 "Failed to unlink %s, ignoring: %m", fn
);
1427 server_refresh_idle_timer(s
);
1431 int server_process_datagram(
1432 sd_event_source
*es
,
1437 size_t label_len
= 0, m
;
1438 Server
*s
= ASSERT_PTR(userdata
);
1439 struct ucred
*ucred
= NULL
;
1440 struct timeval tv_buf
, *tv
= NULL
;
1441 struct cmsghdr
*cmsg
;
1445 int *fds
= NULL
, v
= 0;
1448 /* We use NAME_MAX space for the SELinux label here. The kernel currently enforces no limit, but
1449 * according to suggestions from the SELinux people this will change and it will probably be
1450 * identical to NAME_MAX. For now we use that, but this should be updated one day when the final
1453 * Here, we need to explicitly initialize the buffer with zero, as glibc has a bug in
1454 * __convert_scm_timestamps(), which assumes the buffer is initialized. See #20741. */
1455 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred
)) +
1456 CMSG_SPACE_TIMEVAL
+
1457 CMSG_SPACE(sizeof(int)) + /* fd */
1458 CMSG_SPACE(NAME_MAX
) /* selinux label */) control
= {};
1460 union sockaddr_union sa
= {};
1462 struct msghdr msghdr
= {
1465 .msg_control
= &control
,
1466 .msg_controllen
= sizeof(control
),
1468 .msg_namelen
= sizeof(sa
),
1471 assert(fd
== s
->native_fd
|| fd
== s
->syslog_fd
|| fd
== s
->audit_fd
);
1473 if (revents
!= EPOLLIN
)
1474 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
1475 "Got invalid event from epoll for datagram fd: %" PRIx32
,
1478 /* Try to get the right size, if we can. (Not all sockets support SIOCINQ, hence we just try, but don't rely on
1480 (void) ioctl(fd
, SIOCINQ
, &v
);
1482 /* Fix it up, if it is too small. We use the same fixed value as auditd here. Awful! */
1483 m
= PAGE_ALIGN(MAX3((size_t) v
+ 1,
1485 ALIGN(sizeof(struct nlmsghdr
)) + ALIGN((size_t) MAX_AUDIT_MESSAGE_LENGTH
)) + 1);
1487 if (!GREEDY_REALLOC(s
->buffer
, m
))
1490 iovec
= IOVEC_MAKE(s
->buffer
, MALLOC_ELEMENTSOF(s
->buffer
) - 1); /* Leave room for trailing NUL we add later */
1492 n
= recvmsg_safe(fd
, &msghdr
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
1494 if (ERRNO_IS_TRANSIENT(n
))
1497 log_ratelimit_warning(JOURNAL_LOG_RATELIMIT
,
1498 "Got message with truncated control data (too many fds sent?), ignoring.");
1501 return log_ratelimit_error_errno(n
, JOURNAL_LOG_RATELIMIT
, "recvmsg() failed: %m");
1504 CMSG_FOREACH(cmsg
, &msghdr
)
1505 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1506 cmsg
->cmsg_type
== SCM_CREDENTIALS
&&
1507 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
))) {
1509 ucred
= CMSG_TYPED_DATA(cmsg
, struct ucred
);
1510 } else if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1511 cmsg
->cmsg_type
== SCM_SECURITY
) {
1513 label
= CMSG_TYPED_DATA(cmsg
, char);
1514 label_len
= cmsg
->cmsg_len
- CMSG_LEN(0);
1515 } else if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1516 cmsg
->cmsg_type
== SCM_TIMESTAMP
&&
1517 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct timeval
))) {
1519 tv
= memcpy(&tv_buf
, CMSG_DATA(cmsg
), sizeof(struct timeval
));
1520 } else if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1521 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1523 fds
= CMSG_TYPED_DATA(cmsg
, int);
1524 n_fds
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
1527 /* And a trailing NUL, just in case */
1530 if (fd
== s
->syslog_fd
) {
1531 if (n
> 0 && n_fds
== 0)
1532 server_process_syslog_message(s
, s
->buffer
, n
, ucred
, tv
, label
, label_len
);
1534 log_ratelimit_warning(JOURNAL_LOG_RATELIMIT
,
1535 "Got file descriptors via syslog socket. Ignoring.");
1537 } else if (fd
== s
->native_fd
) {
1538 if (n
> 0 && n_fds
== 0)
1539 server_process_native_message(s
, s
->buffer
, n
, ucred
, tv
, label
, label_len
);
1540 else if (n
== 0 && n_fds
== 1)
1541 (void) server_process_native_file(s
, fds
[0], ucred
, tv
, label
, label_len
);
1543 log_ratelimit_warning(JOURNAL_LOG_RATELIMIT
,
1544 "Got too many file descriptors via native socket. Ignoring.");
1547 assert(fd
== s
->audit_fd
);
1549 if (n
> 0 && n_fds
== 0)
1550 server_process_audit_message(s
, s
->buffer
, n
, ucred
, &sa
, msghdr
.msg_namelen
);
1552 log_ratelimit_warning(JOURNAL_LOG_RATELIMIT
,
1553 "Got file descriptors via audit socket. Ignoring.");
1556 close_many(fds
, n_fds
);
1558 server_refresh_idle_timer(s
);
1562 static void server_full_flush(Server
*s
) {
1565 (void) server_flush_to_var(s
, false);
1567 server_vacuum(s
, false);
1569 server_space_usage_message(s
, NULL
);
1571 server_refresh_idle_timer(s
);
1574 static int dispatch_sigusr1(sd_event_source
*es
, const struct signalfd_siginfo
*si
, void *userdata
) {
1575 Server
*s
= ASSERT_PTR(userdata
);
1578 log_error("Received SIGUSR1 signal from PID %u, but flushing runtime journals not supported for namespaced instances.", si
->ssi_pid
);
1582 log_info("Received SIGUSR1 signal from PID %u, as request to flush runtime journal.", si
->ssi_pid
);
1583 server_full_flush(s
);
1588 static void server_full_rotate(Server
*s
) {
1595 server_vacuum(s
, true);
1597 if (s
->system_journal
)
1598 patch_min_use(&s
->system_storage
);
1599 if (s
->runtime_journal
)
1600 patch_min_use(&s
->runtime_storage
);
1602 /* Let clients know when the most recent rotation happened. */
1603 fn
= strjoina(s
->runtime_directory
, "/rotated");
1604 r
= write_timestamp_file_atomic(fn
, now(CLOCK_MONOTONIC
));
1606 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
1607 "Failed to write %s, ignoring: %m", fn
);
1610 static int dispatch_sigusr2(sd_event_source
*es
, const struct signalfd_siginfo
*si
, void *userdata
) {
1611 Server
*s
= ASSERT_PTR(userdata
);
1613 log_info("Received SIGUSR2 signal from PID %u, as request to rotate journal, rotating.", si
->ssi_pid
);
1614 server_full_rotate(s
);
1619 static int dispatch_sigterm(sd_event_source
*es
, const struct signalfd_siginfo
*si
, void *userdata
) {
1620 _cleanup_(sd_event_source_disable_unrefp
) sd_event_source
*news
= NULL
;
1621 Server
*s
= ASSERT_PTR(userdata
);
1624 log_received_signal(LOG_INFO
, si
);
1626 (void) sd_event_source_set_enabled(es
, SD_EVENT_OFF
); /* Make sure this handler is called at most once */
1628 /* So on one hand we want to ensure that SIGTERMs are definitely handled in appropriate, bounded
1629 * time. On the other hand we want that everything pending is first comprehensively processed and
1630 * written to disk. These goals are incompatible, hence we try to find a middle ground: we'll process
1631 * SIGTERM with high priority, but from the handler (this one right here) we'll install two new event
1632 * sources: one low priority idle one that will issue the exit once everything else is processed (and
1633 * which is hopefully the regular, clean codepath); and one high priority timer that acts as safety
1634 * net: if our idle handler isn't run within 10s, we'll exit anyway.
1636 * TLDR: we'll exit either when everything is processed, or after 10s max, depending on what happens
1639 * Note that exiting before the idle event is hit doesn't typically mean that we lose any data, as
1640 * messages will remain queued in the sockets they came in from, and thus can be processed when we
1641 * start up next – unless we are going down for the final system shutdown, in which case everything
1644 r
= sd_event_add_defer(s
->event
, &news
, NULL
, NULL
); /* NULL handler means → exit when triggered */
1646 log_error_errno(r
, "Failed to allocate exit idle event handler: %m");
1650 (void) sd_event_source_set_description(news
, "exit-idle");
1652 /* Run everything relevant before this. */
1653 r
= sd_event_source_set_priority(news
, SD_EVENT_PRIORITY_NORMAL
+20);
1655 log_error_errno(r
, "Failed to adjust priority of exit idle event handler: %m");
1659 /* Give up ownership, so that this event source is freed automatically when the event loop is freed. */
1660 r
= sd_event_source_set_floating(news
, true);
1662 log_error_errno(r
, "Failed to make exit idle event handler floating: %m");
1666 news
= sd_event_source_unref(news
);
1668 r
= sd_event_add_time_relative(s
->event
, &news
, CLOCK_MONOTONIC
, 10 * USEC_PER_SEC
, 0, NULL
, NULL
);
1670 log_error_errno(r
, "Failed to allocate exit timeout event handler: %m");
1674 (void) sd_event_source_set_description(news
, "exit-timeout");
1676 r
= sd_event_source_set_priority(news
, SD_EVENT_PRIORITY_IMPORTANT
-20); /* This is a safety net, with highest priority */
1678 log_error_errno(r
, "Failed to adjust priority of exit timeout event handler: %m");
1682 r
= sd_event_source_set_floating(news
, true);
1684 log_error_errno(r
, "Failed to make exit timeout event handler floating: %m");
1688 news
= sd_event_source_unref(news
);
1690 log_debug("Exit event sources are now pending.");
1694 sd_event_exit(s
->event
, 0);
1698 static void server_full_sync(Server
*s
) {
1706 /* Let clients know when the most recent sync happened. */
1707 fn
= strjoina(s
->runtime_directory
, "/synced");
1708 r
= write_timestamp_file_atomic(fn
, now(CLOCK_MONOTONIC
));
1710 log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
,
1711 "Failed to write %s, ignoring: %m", fn
);
1716 static int dispatch_sigrtmin1(sd_event_source
*es
, const struct signalfd_siginfo
*si
, void *userdata
) {
1717 Server
*s
= ASSERT_PTR(userdata
);
1719 log_debug("Received SIGRTMIN1 signal from PID %u, as request to sync.", si
->ssi_pid
);
1720 server_full_sync(s
);
1725 static int server_setup_signals(Server
*s
) {
1730 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, SIGUSR1
, SIGUSR2
, SIGRTMIN
+1, SIGRTMIN
+18) >= 0);
1732 r
= sd_event_add_signal(s
->event
, &s
->sigusr1_event_source
, SIGUSR1
, dispatch_sigusr1
, s
);
1736 r
= sd_event_add_signal(s
->event
, &s
->sigusr2_event_source
, SIGUSR2
, dispatch_sigusr2
, s
);
1740 r
= sd_event_add_signal(s
->event
, &s
->sigterm_event_source
, SIGTERM
, dispatch_sigterm
, s
);
1744 /* Let's process SIGTERM early, so that we definitely react to it */
1745 r
= sd_event_source_set_priority(s
->sigterm_event_source
, SD_EVENT_PRIORITY_IMPORTANT
-10);
1749 /* When journald is invoked on the terminal (when debugging), it's useful if C-c is handled
1750 * equivalent to SIGTERM. */
1751 r
= sd_event_add_signal(s
->event
, &s
->sigint_event_source
, SIGINT
, dispatch_sigterm
, s
);
1755 r
= sd_event_source_set_priority(s
->sigint_event_source
, SD_EVENT_PRIORITY_IMPORTANT
-10);
1759 /* SIGRTMIN+1 causes an immediate sync. We process this very late, so that everything else queued at
1760 * this point is really written to disk. Clients can watch /run/systemd/journal/synced with inotify
1761 * until its mtime changes to see when a sync happened. */
1762 r
= sd_event_add_signal(s
->event
, &s
->sigrtmin1_event_source
, SIGRTMIN
+1, dispatch_sigrtmin1
, s
);
1766 r
= sd_event_source_set_priority(s
->sigrtmin1_event_source
, SD_EVENT_PRIORITY_NORMAL
+15);
1770 r
= sd_event_add_signal(s
->event
, NULL
, SIGRTMIN
+18, sigrtmin18_handler
, &s
->sigrtmin18_info
);
1777 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
1778 Server
*s
= ASSERT_PTR(data
);
1781 if (proc_cmdline_key_streq(key
, "systemd.journald.forward_to_syslog")) {
1783 r
= value
? parse_boolean(value
) : true;
1785 log_warning("Failed to parse forward to syslog switch \"%s\". Ignoring.", value
);
1787 s
->forward_to_syslog
= r
;
1789 } else if (proc_cmdline_key_streq(key
, "systemd.journald.forward_to_kmsg")) {
1791 r
= value
? parse_boolean(value
) : true;
1793 log_warning("Failed to parse forward to kmsg switch \"%s\". Ignoring.", value
);
1795 s
->forward_to_kmsg
= r
;
1797 } else if (proc_cmdline_key_streq(key
, "systemd.journald.forward_to_console")) {
1799 r
= value
? parse_boolean(value
) : true;
1801 log_warning("Failed to parse forward to console switch \"%s\". Ignoring.", value
);
1803 s
->forward_to_console
= r
;
1805 } else if (proc_cmdline_key_streq(key
, "systemd.journald.forward_to_wall")) {
1807 r
= value
? parse_boolean(value
) : true;
1809 log_warning("Failed to parse forward to wall switch \"%s\". Ignoring.", value
);
1811 s
->forward_to_wall
= r
;
1813 } else if (proc_cmdline_key_streq(key
, "systemd.journald.max_level_console")) {
1815 if (proc_cmdline_value_missing(key
, value
))
1818 r
= log_level_from_string(value
);
1820 log_warning("Failed to parse max level console value \"%s\". Ignoring.", value
);
1822 s
->max_level_console
= r
;
1824 } else if (proc_cmdline_key_streq(key
, "systemd.journald.max_level_store")) {
1826 if (proc_cmdline_value_missing(key
, value
))
1829 r
= log_level_from_string(value
);
1831 log_warning("Failed to parse max level store value \"%s\". Ignoring.", value
);
1833 s
->max_level_store
= r
;
1835 } else if (proc_cmdline_key_streq(key
, "systemd.journald.max_level_syslog")) {
1837 if (proc_cmdline_value_missing(key
, value
))
1840 r
= log_level_from_string(value
);
1842 log_warning("Failed to parse max level syslog value \"%s\". Ignoring.", value
);
1844 s
->max_level_syslog
= r
;
1846 } else if (proc_cmdline_key_streq(key
, "systemd.journald.max_level_kmsg")) {
1848 if (proc_cmdline_value_missing(key
, value
))
1851 r
= log_level_from_string(value
);
1853 log_warning("Failed to parse max level kmsg value \"%s\". Ignoring.", value
);
1855 s
->max_level_kmsg
= r
;
1857 } else if (proc_cmdline_key_streq(key
, "systemd.journald.max_level_wall")) {
1859 if (proc_cmdline_value_missing(key
, value
))
1862 r
= log_level_from_string(value
);
1864 log_warning("Failed to parse max level wall value \"%s\". Ignoring.", value
);
1866 s
->max_level_wall
= r
;
1868 } else if (proc_cmdline_key_streq(key
, "systemd.journald.max_level_socket")) {
1870 if (proc_cmdline_value_missing(key
, value
))
1873 r
= log_level_from_string(value
);
1875 log_warning("Failed to parse max level socket value \"%s\". Ignoring.", value
);
1877 s
->max_level_socket
= r
;
1879 } else if (startswith(key
, "systemd.journald"))
1880 log_warning("Unknown journald kernel command line option \"%s\". Ignoring.", key
);
1882 /* do not warn about state here, since probably systemd already did */
1886 static int server_parse_config_file(Server
*s
) {
1887 const char *conf_file
;
1892 conf_file
= strjoina("systemd/journald@", s
->namespace, ".conf");
1894 conf_file
= "systemd/journald.conf";
1896 return config_parse_standard_file_with_dropins(
1899 config_item_perf_lookup
, journald_gperf_lookup
,
1904 static int server_dispatch_sync(sd_event_source
*es
, usec_t t
, void *userdata
) {
1905 Server
*s
= ASSERT_PTR(userdata
);
1911 int server_schedule_sync(Server
*s
, int priority
) {
1916 if (priority
<= LOG_CRIT
) {
1917 /* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */
1922 if (!s
->event
|| sd_event_get_state(s
->event
) == SD_EVENT_FINISHED
) {
1923 /* Shutting down the server? Let's sync immediately. */
1928 if (s
->sync_scheduled
)
1931 if (s
->sync_interval_usec
> 0) {
1933 if (!s
->sync_event_source
) {
1934 r
= sd_event_add_time_relative(
1936 &s
->sync_event_source
,
1938 s
->sync_interval_usec
, 0,
1939 server_dispatch_sync
, s
);
1943 r
= sd_event_source_set_priority(s
->sync_event_source
, SD_EVENT_PRIORITY_IMPORTANT
);
1945 r
= sd_event_source_set_time_relative(s
->sync_event_source
, s
->sync_interval_usec
);
1949 r
= sd_event_source_set_enabled(s
->sync_event_source
, SD_EVENT_ONESHOT
);
1954 s
->sync_scheduled
= true;
1960 static int dispatch_hostname_change(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
1961 Server
*s
= ASSERT_PTR(userdata
);
1963 server_cache_hostname(s
);
1967 static int server_open_hostname(Server
*s
) {
1972 s
->hostname_fd
= open("/proc/sys/kernel/hostname",
1973 O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
1974 if (s
->hostname_fd
< 0)
1975 return log_error_errno(errno
, "Failed to open /proc/sys/kernel/hostname: %m");
1977 r
= sd_event_add_io(s
->event
, &s
->hostname_event_source
, s
->hostname_fd
, 0, dispatch_hostname_change
, s
);
1979 /* kernels prior to 3.2 don't support polling this file. Ignore
1982 log_warning_errno(r
, "Failed to register hostname fd in event loop, ignoring: %m");
1983 s
->hostname_fd
= safe_close(s
->hostname_fd
);
1987 return log_error_errno(r
, "Failed to register hostname fd in event loop: %m");
1990 r
= sd_event_source_set_priority(s
->hostname_event_source
, SD_EVENT_PRIORITY_IMPORTANT
-10);
1992 return log_error_errno(r
, "Failed to adjust priority of hostname event source: %m");
1997 static int dispatch_notify_event(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
1998 Server
*s
= ASSERT_PTR(userdata
);
2001 assert(s
->notify_event_source
== es
);
2002 assert(s
->notify_fd
== fd
);
2004 /* The $NOTIFY_SOCKET is writable again, now send exactly one
2005 * message on it. Either it's the watchdog event, the initial
2006 * READY=1 event or an stdout stream event. If there's nothing
2007 * to write anymore, turn our event source off. The next time
2008 * there's something to send it will be turned on again. */
2010 if (!s
->sent_notify_ready
) {
2011 static const char p
[] = "READY=1\n"
2012 "STATUS=Processing requests...";
2014 if (send(s
->notify_fd
, p
, strlen(p
), MSG_DONTWAIT
) < 0) {
2015 if (errno
== EAGAIN
)
2018 return log_error_errno(errno
, "Failed to send READY=1 notification message: %m");
2021 s
->sent_notify_ready
= true;
2022 log_debug("Sent READY=1 notification.");
2024 } else if (s
->send_watchdog
) {
2025 static const char p
[] = "WATCHDOG=1";
2027 if (send(s
->notify_fd
, p
, strlen(p
), MSG_DONTWAIT
) < 0) {
2028 if (errno
== EAGAIN
)
2031 return log_error_errno(errno
, "Failed to send WATCHDOG=1 notification message: %m");
2034 s
->send_watchdog
= false;
2035 log_debug("Sent WATCHDOG=1 notification.");
2037 } else if (s
->stdout_streams_notify_queue
)
2038 /* Dispatch one stream notification event */
2039 stdout_stream_send_notify(s
->stdout_streams_notify_queue
);
2041 /* Leave us enabled if there's still more to do. */
2042 if (s
->send_watchdog
|| s
->stdout_streams_notify_queue
)
2045 /* There was nothing to do anymore, let's turn ourselves off. */
2046 r
= sd_event_source_set_enabled(es
, SD_EVENT_OFF
);
2048 return log_error_errno(r
, "Failed to turn off notify event source: %m");
2053 static int dispatch_watchdog(sd_event_source
*es
, uint64_t usec
, void *userdata
) {
2054 Server
*s
= ASSERT_PTR(userdata
);
2057 s
->send_watchdog
= true;
2059 r
= sd_event_source_set_enabled(s
->notify_event_source
, SD_EVENT_ON
);
2061 log_warning_errno(r
, "Failed to turn on notify event source: %m");
2063 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec
+ s
->watchdog_usec
/ 2);
2065 return log_error_errno(r
, "Failed to restart watchdog event source: %m");
2067 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ON
);
2069 return log_error_errno(r
, "Failed to enable watchdog event source: %m");
2074 static int server_connect_notify(Server
*s
) {
2075 union sockaddr_union sa
;
2081 assert(s
->notify_fd
< 0);
2082 assert(!s
->notify_event_source
);
2085 * So here's the problem: we'd like to send notification messages to PID 1, but we cannot do that via
2086 * sd_notify(), since that's synchronous, and we might end up blocking on it. Specifically: given
2087 * that PID 1 might block on dbus-daemon during IPC, and dbus-daemon is logging to us, and might
2088 * hence block on us, we might end up in a deadlock if we block on sending PID 1 notification
2089 * messages — by generating a full blocking circle. To avoid this, let's create a non-blocking
2090 * socket, and connect it to the notification socket, and then wait for POLLOUT before we send
2091 * anything. This should efficiently avoid any deadlocks, as we'll never block on PID 1, hence PID 1
2092 * can safely block on dbus-daemon which can safely block on us again.
2094 * Don't think that this issue is real? It is, see: https://github.com/systemd/systemd/issues/1505
2097 e
= getenv("NOTIFY_SOCKET");
2101 r
= sockaddr_un_set_path(&sa
.un
, e
);
2103 return log_error_errno(r
, "NOTIFY_SOCKET set to invalid value '%s': %m", e
);
2106 s
->notify_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
2107 if (s
->notify_fd
< 0)
2108 return log_error_errno(errno
, "Failed to create notify socket: %m");
2110 (void) fd_inc_sndbuf(s
->notify_fd
, NOTIFY_SNDBUF_SIZE
);
2112 r
= connect(s
->notify_fd
, &sa
.sa
, sa_len
);
2114 return log_error_errno(errno
, "Failed to connect to notify socket: %m");
2116 r
= sd_event_add_io(s
->event
, &s
->notify_event_source
, s
->notify_fd
, EPOLLOUT
, dispatch_notify_event
, s
);
2118 return log_error_errno(r
, "Failed to watch notification socket: %m");
2120 if (sd_watchdog_enabled(false, &s
->watchdog_usec
) > 0) {
2121 s
->send_watchdog
= true;
2123 r
= sd_event_add_time_relative(s
->event
, &s
->watchdog_event_source
, CLOCK_MONOTONIC
, s
->watchdog_usec
/2, s
->watchdog_usec
/4, dispatch_watchdog
, s
);
2125 return log_error_errno(r
, "Failed to add watchdog time event: %m");
2128 /* This should fire pretty soon, which we'll use to send the READY=1 event. */
2133 static int synchronize_second_half(sd_event_source
*event_source
, void *userdata
) {
2134 Varlink
*link
= ASSERT_PTR(userdata
);
2138 assert_se(s
= varlink_get_userdata(link
));
2140 /* This is the "second half" of the Synchronize() varlink method. This function is called as deferred
2141 * event source at a low priority to ensure the synchronization completes after all queued log
2142 * messages are processed. */
2143 server_full_sync(s
);
2145 /* Let's get rid of the event source now, by marking it as non-floating again. It then has no ref
2146 * anymore and is immediately destroyed after we return from this function, i.e. from this event
2147 * source handler at the end. */
2148 r
= sd_event_source_set_floating(event_source
, false);
2150 return log_error_errno(r
, "Failed to mark event source as non-floating: %m");
2152 return varlink_reply(link
, NULL
);
2155 static void synchronize_destroy(void *userdata
) {
2156 varlink_unref(userdata
);
2159 static int vl_method_synchronize(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
2160 _cleanup_(sd_event_source_unrefp
) sd_event_source
*event_source
= NULL
;
2161 Server
*s
= ASSERT_PTR(userdata
);
2166 if (json_variant_elements(parameters
) > 0)
2167 return varlink_error_invalid_parameter(link
, parameters
);
2169 log_info("Received client request to sync journal.");
2171 /* We don't do the main work now, but instead enqueue a deferred event loop job which will do
2172 * it. That job is scheduled at low priority, so that we return from this method call only after all
2173 * queued but not processed log messages are written to disk, so that this method call returning can
2174 * be used as nice synchronization point. */
2175 r
= sd_event_add_defer(s
->event
, &event_source
, synchronize_second_half
, link
);
2177 return log_error_errno(r
, "Failed to allocate defer event source: %m");
2179 r
= sd_event_source_set_destroy_callback(event_source
, synchronize_destroy
);
2181 return log_error_errno(r
, "Failed to set event source destroy callback: %m");
2183 varlink_ref(link
); /* The varlink object is now left to the destroy callback to unref */
2185 r
= sd_event_source_set_priority(event_source
, SD_EVENT_PRIORITY_NORMAL
+15);
2187 return log_error_errno(r
, "Failed to set defer event source priority: %m");
2189 /* Give up ownership of this event source. It will now be destroyed along with event loop itself,
2190 * unless it destroys itself earlier. */
2191 r
= sd_event_source_set_floating(event_source
, true);
2193 return log_error_errno(r
, "Failed to mark event source as floating: %m");
2195 (void) sd_event_source_set_description(event_source
, "deferred-sync");
2200 static int vl_method_rotate(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
2201 Server
*s
= ASSERT_PTR(userdata
);
2205 if (json_variant_elements(parameters
) > 0)
2206 return varlink_error_invalid_parameter(link
, parameters
);
2208 log_info("Received client request to rotate journal, rotating.");
2209 server_full_rotate(s
);
2211 return varlink_reply(link
, NULL
);
2214 static int vl_method_flush_to_var(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
2215 Server
*s
= ASSERT_PTR(userdata
);
2219 if (json_variant_elements(parameters
) > 0)
2220 return varlink_error_invalid_parameter(link
, parameters
);
2222 return varlink_error(link
, "io.systemd.Journal.NotSupportedByNamespaces", NULL
);
2224 log_info("Received client request to flush runtime journal.");
2225 server_full_flush(s
);
2227 return varlink_reply(link
, NULL
);
2230 static int vl_method_relinquish_var(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
2231 Server
*s
= ASSERT_PTR(userdata
);
2235 if (json_variant_elements(parameters
) > 0)
2236 return varlink_error_invalid_parameter(link
, parameters
);
2238 return varlink_error(link
, "io.systemd.Journal.NotSupportedByNamespaces", NULL
);
2240 log_info("Received client request to relinquish %s access.", s
->system_storage
.path
);
2241 server_relinquish_var(s
);
2243 return varlink_reply(link
, NULL
);
2246 static int vl_connect(VarlinkServer
*server
, Varlink
*link
, void *userdata
) {
2247 Server
*s
= ASSERT_PTR(userdata
);
2252 (void) server_start_or_stop_idle_timer(s
); /* maybe we are no longer idle */
2257 static void vl_disconnect(VarlinkServer
*server
, Varlink
*link
, void *userdata
) {
2258 Server
*s
= ASSERT_PTR(userdata
);
2263 (void) server_start_or_stop_idle_timer(s
); /* maybe we are idle now */
2266 static int server_open_varlink(Server
*s
, const char *socket
, int fd
) {
2271 r
= varlink_server_new(&s
->varlink_server
, VARLINK_SERVER_ROOT_ONLY
|VARLINK_SERVER_INHERIT_USERDATA
);
2275 varlink_server_set_userdata(s
->varlink_server
, s
);
2277 r
= varlink_server_add_interface(s
->varlink_server
, &vl_interface_io_systemd_Journal
);
2279 return log_error_errno(r
, "Failed to add Journal interface to varlink server: %m");
2281 r
= varlink_server_bind_method_many(
2283 "io.systemd.Journal.Synchronize", vl_method_synchronize
,
2284 "io.systemd.Journal.Rotate", vl_method_rotate
,
2285 "io.systemd.Journal.FlushToVar", vl_method_flush_to_var
,
2286 "io.systemd.Journal.RelinquishVar", vl_method_relinquish_var
);
2290 r
= varlink_server_bind_connect(s
->varlink_server
, vl_connect
);
2294 r
= varlink_server_bind_disconnect(s
->varlink_server
, vl_disconnect
);
2299 r
= varlink_server_listen_address(s
->varlink_server
, socket
, 0600);
2301 r
= varlink_server_listen_fd(s
->varlink_server
, fd
);
2305 r
= varlink_server_attach_event(s
->varlink_server
, s
->event
, SD_EVENT_PRIORITY_NORMAL
);
2312 int server_map_seqnum_file(
2318 _cleanup_free_
char *fn
= NULL
;
2319 _cleanup_close_
int fd
= -EBADF
;
2328 fn
= path_join(s
->runtime_directory
, fname
);
2332 fd
= open(fn
, O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
2336 r
= posix_fallocate_loop(fd
, 0, size
);
2340 p
= mmap(NULL
, size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
2341 if (p
== MAP_FAILED
)
2348 void server_unmap_seqnum_file(void *p
, size_t size
) {
2354 assert_se(munmap(p
, size
) >= 0);
2357 static bool server_is_idle(Server
*s
) {
2360 /* The server for the main namespace is never idle */
2364 /* If a retention maximum is set larger than the idle time we need to be running to enforce it, hence
2365 * turn off the idle logic. */
2366 if (s
->max_retention_usec
> IDLE_TIMEOUT_USEC
)
2369 /* We aren't idle if we have a varlink client */
2370 if (varlink_server_current_connections(s
->varlink_server
) > 0)
2373 /* If we have stdout streams we aren't idle */
2374 if (s
->n_stdout_streams
> 0)
2380 static int server_idle_handler(sd_event_source
*source
, uint64_t usec
, void *userdata
) {
2381 Server
*s
= ASSERT_PTR(userdata
);
2385 log_debug("Server is idle, exiting.");
2386 sd_event_exit(s
->event
, 0);
2390 int server_start_or_stop_idle_timer(Server
*s
) {
2391 _cleanup_(sd_event_source_unrefp
) sd_event_source
*source
= NULL
;
2396 if (!server_is_idle(s
)) {
2397 s
->idle_event_source
= sd_event_source_disable_unref(s
->idle_event_source
);
2401 if (s
->idle_event_source
)
2404 r
= sd_event_add_time_relative(s
->event
, &source
, CLOCK_MONOTONIC
, IDLE_TIMEOUT_USEC
, 0, server_idle_handler
, s
);
2406 return log_error_errno(r
, "Failed to allocate idle timer: %m");
2408 r
= sd_event_source_set_priority(source
, SD_EVENT_PRIORITY_IDLE
);
2410 return log_error_errno(r
, "Failed to set idle timer priority: %m");
2412 (void) sd_event_source_set_description(source
, "idle-timer");
2414 s
->idle_event_source
= TAKE_PTR(source
);
2418 int server_refresh_idle_timer(Server
*s
) {
2423 if (!s
->idle_event_source
)
2426 r
= sd_event_source_set_time_relative(s
->idle_event_source
, IDLE_TIMEOUT_USEC
);
2428 return log_error_errno(r
, "Failed to refresh idle timer: %m");
2433 static int server_set_namespace(Server
*s
, const char *namespace) {
2439 if (!log_namespace_name_valid(namespace))
2440 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified namespace name not valid, refusing: %s", namespace);
2442 s
->namespace = strdup(namespace);
2446 s
->namespace_field
= strjoin("_NAMESPACE=", namespace);
2447 if (!s
->namespace_field
)
2453 static int server_memory_pressure(sd_event_source
*es
, void *userdata
) {
2454 Server
*s
= ASSERT_PTR(userdata
);
2456 log_info("Under memory pressure, flushing caches.");
2458 /* Flushed the cached info we might have about client processes */
2459 client_context_flush_regular(s
);
2461 /* Let's also close all user files (but keep the system/runtime one open) */
2463 JournalFile
*first
= ordered_hashmap_steal_first(s
->user_journals
);
2468 (void) journal_file_offline_close(first
);
2471 sd_event_trim_memory();
2476 static int server_setup_memory_pressure(Server
*s
) {
2481 r
= sd_event_add_memory_pressure(s
->event
, NULL
, server_memory_pressure
, s
);
2483 log_full_errno(ERRNO_IS_NOT_SUPPORTED(r
) || ERRNO_IS_PRIVILEGE(r
) || (r
== -EHOSTDOWN
) ? LOG_DEBUG
: LOG_NOTICE
, r
,
2484 "Failed to install memory pressure event source, ignoring: %m");
2489 static void server_load_credentials(Server
*s
) {
2490 _cleanup_free_
void *data
= NULL
;
2495 /* if we already have a forward address from config don't load the credential */
2496 if (s
->forward_to_socket
.sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
2497 return log_debug("Socket forward address already set not loading journal.forward_to_socket");
2499 r
= read_credential("journal.forward_to_socket", &data
, NULL
);
2501 return (void) log_debug_errno(r
, "Failed to read credential journal.forward_to_socket, ignoring: %m");
2503 r
= socket_address_parse(&s
->forward_to_socket
, data
);
2505 log_debug_errno(r
, "Failed to parse credential journal.forward_to_socket, ignoring: %m");
2508 int server_new(Server
**ret
) {
2509 _cleanup_(server_freep
) Server
*s
= NULL
;
2518 .syslog_fd
= -EBADF
,
2519 .native_fd
= -EBADF
,
2520 .stdout_fd
= -EBADF
,
2521 .dev_kmsg_fd
= -EBADF
,
2523 .hostname_fd
= -EBADF
,
2524 .notify_fd
= -EBADF
,
2525 .forward_socket_fd
= -EBADF
,
2527 .compress
.enabled
= true,
2528 .compress
.threshold_bytes
= UINT64_MAX
,
2533 .watchdog_usec
= USEC_INFINITY
,
2535 .sync_interval_usec
= DEFAULT_SYNC_INTERVAL_USEC
,
2536 .sync_scheduled
= false,
2538 .ratelimit_interval
= DEFAULT_RATE_LIMIT_INTERVAL
,
2539 .ratelimit_burst
= DEFAULT_RATE_LIMIT_BURST
,
2541 .forward_to_wall
= true,
2542 .forward_to_socket
= { .sockaddr
.sa
.sa_family
= AF_UNSPEC
},
2544 .max_file_usec
= DEFAULT_MAX_FILE_USEC
,
2546 .max_level_store
= LOG_DEBUG
,
2547 .max_level_syslog
= LOG_DEBUG
,
2548 .max_level_kmsg
= LOG_NOTICE
,
2549 .max_level_console
= LOG_INFO
,
2550 .max_level_wall
= LOG_EMERG
,
2551 .max_level_socket
= LOG_DEBUG
,
2553 .line_max
= DEFAULT_LINE_MAX
,
2555 .runtime_storage
.name
= "Runtime Journal",
2556 .system_storage
.name
= "System Journal",
2558 .kmsg_own_ratelimit
= {
2559 .interval
= DEFAULT_KMSG_OWN_INTERVAL
,
2560 .burst
= DEFAULT_KMSG_OWN_BURST
,
2563 .sigrtmin18_info
.memory_pressure_handler
= server_memory_pressure
,
2564 .sigrtmin18_info
.memory_pressure_userdata
= s
,
2571 int server_init(Server
*s
, const char *namespace) {
2572 const char *native_socket
, *syslog_socket
, *stdout_socket
, *varlink_socket
, *e
;
2573 _cleanup_fdset_free_ FDSet
*fds
= NULL
;
2574 int n
, r
, varlink_fd
= -EBADF
;
2579 r
= server_set_namespace(s
, namespace);
2583 /* By default, only read from /dev/kmsg if are the main namespace */
2584 s
->read_kmsg
= !s
->namespace;
2585 s
->storage
= s
->namespace ? STORAGE_PERSISTENT
: STORAGE_AUTO
;
2587 journal_reset_metrics(&s
->system_storage
.metrics
);
2588 journal_reset_metrics(&s
->runtime_storage
.metrics
);
2590 server_parse_config_file(s
);
2592 server_load_credentials(s
);
2594 if (!s
->namespace) {
2595 /* Parse kernel command line, but only if we are not a namespace instance */
2596 r
= proc_cmdline_parse(parse_proc_cmdline_item
, s
, PROC_CMDLINE_STRIP_RD_PREFIX
);
2598 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
2601 if (!!s
->ratelimit_interval
!= !!s
->ratelimit_burst
) { /* One set to 0 and the other not? */
2602 log_debug("Setting both rate limit interval and burst from "USEC_FMT
",%u to 0,0",
2603 s
->ratelimit_interval
, s
->ratelimit_burst
);
2604 s
->ratelimit_interval
= s
->ratelimit_burst
= 0;
2607 e
= getenv("RUNTIME_DIRECTORY");
2609 s
->runtime_directory
= strdup(e
);
2610 else if (s
->namespace)
2611 s
->runtime_directory
= strjoin("/run/systemd/journal.", s
->namespace);
2613 s
->runtime_directory
= strdup("/run/systemd/journal");
2614 if (!s
->runtime_directory
)
2617 (void) mkdir_p(s
->runtime_directory
, 0755);
2619 s
->user_journals
= ordered_hashmap_new(NULL
);
2620 if (!s
->user_journals
)
2623 s
->mmap
= mmap_cache_new();
2627 s
->deferred_closes
= set_new(NULL
);
2628 if (!s
->deferred_closes
)
2631 r
= sd_event_default(&s
->event
);
2633 return log_error_errno(r
, "Failed to create event loop: %m");
2635 n
= sd_listen_fds(true);
2637 return log_error_errno(n
, "Failed to read listening file descriptors from environment: %m");
2639 native_socket
= strjoina(s
->runtime_directory
, "/socket");
2640 stdout_socket
= strjoina(s
->runtime_directory
, "/stdout");
2641 syslog_socket
= strjoina(s
->runtime_directory
, "/dev-log");
2642 varlink_socket
= strjoina(s
->runtime_directory
, "/io.systemd.journal");
2644 for (int fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++)
2646 if (sd_is_socket_unix(fd
, SOCK_DGRAM
, -1, native_socket
, 0) > 0) {
2648 if (s
->native_fd
>= 0)
2649 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2650 "Too many native sockets passed.");
2654 } else if (sd_is_socket_unix(fd
, SOCK_STREAM
, 1, stdout_socket
, 0) > 0) {
2656 if (s
->stdout_fd
>= 0)
2657 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2658 "Too many stdout sockets passed.");
2662 } else if (sd_is_socket_unix(fd
, SOCK_DGRAM
, -1, syslog_socket
, 0) > 0) {
2664 if (s
->syslog_fd
>= 0)
2665 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2666 "Too many /dev/log sockets passed.");
2670 } else if (sd_is_socket_unix(fd
, SOCK_STREAM
, 1, varlink_socket
, 0) > 0) {
2672 if (varlink_fd
>= 0)
2673 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2674 "Too many varlink sockets passed.");
2677 } else if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
2679 if (s
->audit_fd
>= 0)
2680 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2681 "Too many audit sockets passed.");
2693 r
= fdset_put(fds
, fd
);
2698 /* Try to restore streams, but don't bother if this fails */
2699 (void) server_restore_streams(s
, fds
);
2701 if (!fdset_isempty(fds
)) {
2702 log_warning("%u unknown file descriptors passed, closing.", fdset_size(fds
));
2703 fds
= fdset_free(fds
);
2706 no_sockets
= s
->native_fd
< 0 && s
->stdout_fd
< 0 && s
->syslog_fd
< 0 && s
->audit_fd
< 0 && varlink_fd
< 0;
2708 /* always open stdout, syslog, native, and kmsg sockets */
2710 /* systemd-journald.socket: /run/systemd/journal/stdout */
2711 r
= server_open_stdout_socket(s
, stdout_socket
);
2715 /* systemd-journald-dev-log.socket: /run/systemd/journal/dev-log */
2716 r
= server_open_syslog_socket(s
, syslog_socket
);
2720 /* systemd-journald.socket: /run/systemd/journal/socket */
2721 r
= server_open_native_socket(s
, native_socket
);
2726 r
= server_open_dev_kmsg(s
);
2730 /* Unless we got *some* sockets and not audit, open audit socket */
2731 if (s
->audit_fd
>= 0 || no_sockets
) {
2732 log_info("Collecting audit messages is enabled.");
2734 r
= server_open_audit(s
);
2738 log_info("Collecting audit messages is disabled.");
2740 r
= server_open_varlink(s
, varlink_socket
, varlink_fd
);
2744 r
= server_map_seqnum_file(s
, "seqnum", sizeof(SeqnumData
), (void**) &s
->seqnum
);
2746 return log_error_errno(r
, "Failed to map main seqnum file: %m");
2748 r
= server_open_kernel_seqnum(s
);
2752 r
= server_open_hostname(s
);
2756 r
= server_setup_signals(s
);
2760 r
= server_setup_memory_pressure(s
);
2764 s
->ratelimit
= journal_ratelimit_new();
2768 r
= cg_get_root_path(&s
->cgroup_root
);
2770 return log_error_errno(r
, "Failed to acquire cgroup root path: %m");
2772 server_cache_hostname(s
);
2773 server_cache_boot_id(s
);
2774 server_cache_machine_id(s
);
2777 s
->runtime_storage
.path
= strjoin("/run/log/journal/", SERVER_MACHINE_ID(s
), ".", s
->namespace);
2779 s
->runtime_storage
.path
= strjoin("/run/log/journal/", SERVER_MACHINE_ID(s
));
2780 if (!s
->runtime_storage
.path
)
2783 e
= getenv("LOGS_DIRECTORY");
2785 s
->system_storage
.path
= strdup(e
);
2786 else if (s
->namespace)
2787 s
->system_storage
.path
= strjoin("/var/log/journal/", SERVER_MACHINE_ID(s
), ".", s
->namespace);
2789 s
->system_storage
.path
= strjoin("/var/log/journal/", SERVER_MACHINE_ID(s
));
2790 if (!s
->system_storage
.path
)
2793 (void) server_connect_notify(s
);
2795 (void) client_context_acquire_default(s
);
2797 r
= server_system_journal_open(s
, /* flush_requested= */ false, /* relinquish_requested= */ false);
2801 server_start_or_stop_idle_timer(s
);
2806 void server_maybe_append_tags(Server
*s
) {
2811 n
= now(CLOCK_REALTIME
);
2813 if (s
->system_journal
)
2814 journal_file_maybe_append_tag(s
->system_journal
, n
);
2816 ORDERED_HASHMAP_FOREACH(f
, s
->user_journals
)
2817 journal_file_maybe_append_tag(f
, n
);
2821 Server
* server_free(Server
*s
) {
2826 free(s
->namespace_field
);
2828 set_free_with_destructor(s
->deferred_closes
, journal_file_offline_close
);
2830 while (s
->stdout_streams
)
2831 stdout_stream_free(s
->stdout_streams
);
2833 client_context_flush_all(s
);
2835 (void) journal_file_offline_close(s
->system_journal
);
2836 (void) journal_file_offline_close(s
->runtime_journal
);
2838 ordered_hashmap_free_with_destructor(s
->user_journals
, journal_file_offline_close
);
2840 varlink_server_unref(s
->varlink_server
);
2842 sd_event_source_unref(s
->syslog_event_source
);
2843 sd_event_source_unref(s
->native_event_source
);
2844 sd_event_source_unref(s
->stdout_event_source
);
2845 sd_event_source_unref(s
->dev_kmsg_event_source
);
2846 sd_event_source_unref(s
->audit_event_source
);
2847 sd_event_source_unref(s
->sync_event_source
);
2848 sd_event_source_unref(s
->sigusr1_event_source
);
2849 sd_event_source_unref(s
->sigusr2_event_source
);
2850 sd_event_source_unref(s
->sigterm_event_source
);
2851 sd_event_source_unref(s
->sigint_event_source
);
2852 sd_event_source_unref(s
->sigrtmin1_event_source
);
2853 sd_event_source_unref(s
->hostname_event_source
);
2854 sd_event_source_unref(s
->notify_event_source
);
2855 sd_event_source_unref(s
->watchdog_event_source
);
2856 sd_event_source_unref(s
->idle_event_source
);
2857 sd_event_unref(s
->event
);
2859 safe_close(s
->syslog_fd
);
2860 safe_close(s
->native_fd
);
2861 safe_close(s
->stdout_fd
);
2862 safe_close(s
->dev_kmsg_fd
);
2863 safe_close(s
->audit_fd
);
2864 safe_close(s
->hostname_fd
);
2865 safe_close(s
->notify_fd
);
2866 safe_close(s
->forward_socket_fd
);
2869 journal_ratelimit_free(s
->ratelimit
);
2871 server_unmap_seqnum_file(s
->seqnum
, sizeof(*s
->seqnum
));
2872 server_unmap_seqnum_file(s
->kernel_seqnum
, sizeof(*s
->kernel_seqnum
));
2876 free(s
->cgroup_root
);
2877 free(s
->hostname_field
);
2878 free(s
->runtime_storage
.path
);
2879 free(s
->system_storage
.path
);
2880 free(s
->runtime_directory
);
2882 mmap_cache_unref(s
->mmap
);
2887 static const char* const storage_table
[_STORAGE_MAX
] = {
2888 [STORAGE_AUTO
] = "auto",
2889 [STORAGE_VOLATILE
] = "volatile",
2890 [STORAGE_PERSISTENT
] = "persistent",
2891 [STORAGE_NONE
] = "none"
2894 DEFINE_STRING_TABLE_LOOKUP(storage
, Storage
);
2895 DEFINE_CONFIG_PARSE_ENUM(config_parse_storage
, storage
, Storage
, "Failed to parse storage setting");
2897 static const char* const split_mode_table
[_SPLIT_MAX
] = {
2898 [SPLIT_LOGIN
] = "login",
2899 [SPLIT_UID
] = "uid",
2900 [SPLIT_NONE
] = "none",
2903 DEFINE_STRING_TABLE_LOOKUP(split_mode
, SplitMode
);
2904 DEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode
, split_mode
, SplitMode
, "Failed to parse split mode setting");
2906 int config_parse_line_max(
2908 const char *filename
,
2910 const char *section
,
2911 unsigned section_line
,
2918 size_t *sz
= ASSERT_PTR(data
);
2925 if (isempty(rvalue
))
2926 /* Empty assignment means default */
2927 *sz
= DEFAULT_LINE_MAX
;
2931 r
= parse_size(rvalue
, 1024, &v
);
2933 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse LineMax= value, ignoring: %s", rvalue
);
2938 /* Why specify 79 here as minimum line length? Simply, because the most common traditional
2939 * terminal size is 80ch, and it might make sense to break one character before the natural
2940 * line break would occur on that. */
2941 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "LineMax= too small, clamping to 79: %s", rvalue
);
2943 } else if (v
> (uint64_t) (SSIZE_MAX
-1)) {
2944 /* So, why specify SSIZE_MAX-1 here? Because that's one below the largest size value read()
2945 * can return, and we need one extra byte for the trailing NUL byte. Of course IRL such large
2946 * memory allocations will fail anyway, hence this limit is mostly theoretical anyway, as we'll
2947 * fail much earlier anyway. */
2948 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "LineMax= too large, clamping to %" PRIu64
": %s", (uint64_t) (SSIZE_MAX
-1), rvalue
);
2957 int config_parse_compress(
2959 const char *filename
,
2961 const char *section
,
2962 unsigned section_line
,
2969 JournalCompressOptions
* compress
= ASSERT_PTR(data
);
2976 if (isempty(rvalue
)) {
2977 compress
->enabled
= true;
2978 compress
->threshold_bytes
= UINT64_MAX
;
2979 } else if (streq(rvalue
, "1")) {
2980 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
2981 "Compress= ambiguously specified as 1, enabling compression with default threshold");
2982 compress
->enabled
= true;
2983 } else if (streq(rvalue
, "0")) {
2984 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
2985 "Compress= ambiguously specified as 0, disabling compression");
2986 compress
->enabled
= false;
2988 r
= parse_boolean(rvalue
);
2990 r
= parse_size(rvalue
, 1024, &compress
->threshold_bytes
);
2992 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2993 "Failed to parse Compress= value, ignoring: %s", rvalue
);
2995 compress
->enabled
= true;
2997 compress
->enabled
= r
;
3003 int config_parse_forward_to_socket(
3005 const char *filename
,
3007 const char *section
,
3008 unsigned section_line
,
3015 SocketAddress
* addr
= ASSERT_PTR(data
);
3022 if (isempty(rvalue
))
3023 *addr
= (SocketAddress
) { .sockaddr
.sa
.sa_family
= AF_UNSPEC
};
3025 r
= socket_address_parse(addr
, rvalue
);
3027 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
3028 "Failed to parse ForwardToSocket= value, ignoring: %s", rvalue
);