]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-server.h
journal: Ratelimit more journald log messages
[thirdparty/systemd.git] / src / journal / journald-server.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5 #include <sys/types.h>
6
7 #include "sd-event.h"
8
9 typedef struct Server Server;
10
11 #include "conf-parser.h"
12 #include "hashmap.h"
13 #include "journald-context.h"
14 #include "journald-rate-limit.h"
15 #include "journald-stream.h"
16 #include "list.h"
17 #include "managed-journal-file.h"
18 #include "prioq.h"
19 #include "ratelimit.h"
20 #include "time-util.h"
21 #include "varlink.h"
22
23 #define JOURNALD_LOG_RATELIMIT ((RateLimit) { .interval = 60 * USEC_PER_SEC, .burst = 3 })
24
25 typedef enum Storage {
26 STORAGE_AUTO,
27 STORAGE_VOLATILE,
28 STORAGE_PERSISTENT,
29 STORAGE_NONE,
30 _STORAGE_MAX,
31 _STORAGE_INVALID = -EINVAL,
32 } Storage;
33
34 typedef enum SplitMode {
35 SPLIT_UID,
36 SPLIT_LOGIN, /* deprecated */
37 SPLIT_NONE,
38 _SPLIT_MAX,
39 _SPLIT_INVALID = -EINVAL,
40 } SplitMode;
41
42 typedef struct JournalCompressOptions {
43 bool enabled;
44 uint64_t threshold_bytes;
45 } JournalCompressOptions;
46
47 typedef struct JournalStorageSpace {
48 usec_t timestamp;
49
50 uint64_t available;
51 uint64_t limit;
52
53 uint64_t vfs_used; /* space used by journal files */
54 uint64_t vfs_available;
55 } JournalStorageSpace;
56
57 typedef struct JournalStorage {
58 const char *name;
59 char *path;
60
61 JournalMetrics metrics;
62 JournalStorageSpace space;
63 } JournalStorage;
64
65 struct Server {
66 char *namespace;
67
68 int syslog_fd;
69 int native_fd;
70 int stdout_fd;
71 int dev_kmsg_fd;
72 int audit_fd;
73 int hostname_fd;
74 int notify_fd;
75
76 sd_event *event;
77
78 sd_event_source *syslog_event_source;
79 sd_event_source *native_event_source;
80 sd_event_source *stdout_event_source;
81 sd_event_source *dev_kmsg_event_source;
82 sd_event_source *audit_event_source;
83 sd_event_source *sync_event_source;
84 sd_event_source *sigusr1_event_source;
85 sd_event_source *sigusr2_event_source;
86 sd_event_source *sigterm_event_source;
87 sd_event_source *sigint_event_source;
88 sd_event_source *sigrtmin1_event_source;
89 sd_event_source *hostname_event_source;
90 sd_event_source *notify_event_source;
91 sd_event_source *watchdog_event_source;
92 sd_event_source *idle_event_source;
93
94 ManagedJournalFile *runtime_journal;
95 ManagedJournalFile *system_journal;
96 OrderedHashmap *user_journals;
97
98 uint64_t seqnum;
99
100 char *buffer;
101
102 JournalRateLimit *ratelimit;
103 usec_t sync_interval_usec;
104 usec_t ratelimit_interval;
105 unsigned ratelimit_burst;
106
107 JournalStorage runtime_storage;
108 JournalStorage system_storage;
109
110 JournalCompressOptions compress;
111 bool seal;
112 bool read_kmsg;
113 int set_audit;
114
115 bool forward_to_kmsg;
116 bool forward_to_syslog;
117 bool forward_to_console;
118 bool forward_to_wall;
119
120 unsigned n_forward_syslog_missed;
121 usec_t last_warn_forward_syslog_missed;
122
123 usec_t max_retention_usec;
124 usec_t max_file_usec;
125 usec_t oldest_file_usec;
126
127 LIST_HEAD(StdoutStream, stdout_streams);
128 LIST_HEAD(StdoutStream, stdout_streams_notify_queue);
129 unsigned n_stdout_streams;
130
131 char *tty_path;
132
133 int max_level_store;
134 int max_level_syslog;
135 int max_level_kmsg;
136 int max_level_console;
137 int max_level_wall;
138
139 Storage storage;
140 SplitMode split_mode;
141
142 MMapCache *mmap;
143
144 Set *deferred_closes;
145
146 uint64_t *kernel_seqnum;
147 bool dev_kmsg_readable:1;
148 RateLimit kmsg_own_ratelimit;
149
150 bool send_watchdog:1;
151 bool sent_notify_ready:1;
152 bool sync_scheduled:1;
153
154 char machine_id_field[sizeof("_MACHINE_ID=") + 32];
155 char boot_id_field[sizeof("_BOOT_ID=") + 32];
156 char *hostname_field;
157 char *namespace_field;
158 char *runtime_directory;
159
160 /* Cached cgroup root, so that we don't have to query that all the time */
161 char *cgroup_root;
162
163 usec_t watchdog_usec;
164
165 usec_t last_realtime_clock;
166
167 size_t line_max;
168
169 /* Caching of client metadata */
170 Hashmap *client_contexts;
171 Prioq *client_contexts_lru;
172
173 usec_t last_cache_pid_flush;
174
175 ClientContext *my_context; /* the context of journald itself */
176 ClientContext *pid1_context; /* the context of PID 1 */
177
178 VarlinkServer *varlink_server;
179 };
180
181 #define SERVER_MACHINE_ID(s) ((s)->machine_id_field + STRLEN("_MACHINE_ID="))
182
183 /* Extra fields for any log messages */
184 #define N_IOVEC_META_FIELDS 24
185
186 /* Extra fields for log messages that contain OBJECT_PID= (i.e. log about another process) */
187 #define N_IOVEC_OBJECT_FIELDS 18
188
189 /* Maximum number of fields we'll add in for driver (i.e. internal) messages */
190 #define N_IOVEC_PAYLOAD_FIELDS 16
191
192 /* kmsg: Maximum number of extra fields we'll import from the kernel's /dev/kmsg */
193 #define N_IOVEC_KERNEL_FIELDS 64
194
195 /* kmsg: Maximum number of extra fields we'll import from udev's devices */
196 #define N_IOVEC_UDEV_FIELDS 32
197
198 /* audit: Maximum number of extra fields we'll import from audit messages */
199 #define N_IOVEC_AUDIT_FIELDS 64
200
201 void server_dispatch_message(Server *s, struct iovec *iovec, size_t n, size_t m, ClientContext *c, const struct timeval *tv, int priority, pid_t object_pid);
202 void server_driver_message(Server *s, pid_t object_pid, const char *message_id, const char *format, ...) _sentinel_ _printf_(4,0);
203
204 /* gperf lookup function */
205 const struct ConfigPerfItem* journald_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
206
207 CONFIG_PARSER_PROTOTYPE(config_parse_storage);
208 CONFIG_PARSER_PROTOTYPE(config_parse_line_max);
209 CONFIG_PARSER_PROTOTYPE(config_parse_compress);
210
211 const char *storage_to_string(Storage s) _const_;
212 Storage storage_from_string(const char *s) _pure_;
213
214 CONFIG_PARSER_PROTOTYPE(config_parse_split_mode);
215
216 const char *split_mode_to_string(SplitMode s) _const_;
217 SplitMode split_mode_from_string(const char *s) _pure_;
218
219 int server_init(Server *s, const char *namespace);
220 void server_done(Server *s);
221 void server_sync(Server *s);
222 void server_vacuum(Server *s, bool verbose);
223 void server_rotate(Server *s);
224 int server_schedule_sync(Server *s, int priority);
225 int server_flush_to_var(Server *s, bool require_flag_file);
226 void server_maybe_append_tags(Server *s);
227 int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata);
228 void server_space_usage_message(Server *s, JournalStorage *storage);
229
230 int server_start_or_stop_idle_timer(Server *s);
231 int server_refresh_idle_timer(Server *s);