]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
d025f1e4 | 2 | |
349cc4a5 | 3 | #if HAVE_SELINUX |
24882e06 LP |
4 | #include <selinux/selinux.h> |
5 | #endif | |
8580d1f7 LP |
6 | #include <sys/ioctl.h> |
7 | #include <sys/mman.h> | |
8 | #include <sys/signalfd.h> | |
9 | #include <sys/statvfs.h> | |
07630cea | 10 | #include <linux/sockios.h> |
24882e06 | 11 | |
8580d1f7 | 12 | #include "sd-daemon.h" |
74df0fca LP |
13 | #include "sd-journal.h" |
14 | #include "sd-messages.h" | |
8580d1f7 LP |
15 | |
16 | #include "acl-util.h" | |
b5efdb8a | 17 | #include "alloc-util.h" |
430f0182 | 18 | #include "audit-util.h" |
d025f1e4 | 19 | #include "cgroup-util.h" |
d025f1e4 | 20 | #include "conf-parser.h" |
a0956174 | 21 | #include "dirent-util.h" |
0dec689b | 22 | #include "extract-word.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
33d52ab9 | 24 | #include "fileio.h" |
f97b34a6 | 25 | #include "format-util.h" |
f4f15635 | 26 | #include "fs-util.h" |
8580d1f7 | 27 | #include "hashmap.h" |
958b66ea | 28 | #include "hostname-util.h" |
4b58153d | 29 | #include "id128-util.h" |
afc5dbf3 | 30 | #include "io-util.h" |
8580d1f7 LP |
31 | #include "journal-authenticate.h" |
32 | #include "journal-file.h" | |
d025f1e4 ZJS |
33 | #include "journal-internal.h" |
34 | #include "journal-vacuum.h" | |
8580d1f7 | 35 | #include "journald-audit.h" |
22e3a02b | 36 | #include "journald-context.h" |
d025f1e4 | 37 | #include "journald-kmsg.h" |
d025f1e4 | 38 | #include "journald-native.h" |
8580d1f7 | 39 | #include "journald-rate-limit.h" |
3ffd4af2 | 40 | #include "journald-server.h" |
8580d1f7 LP |
41 | #include "journald-stream.h" |
42 | #include "journald-syslog.h" | |
4b58153d | 43 | #include "log.h" |
f5947a5e | 44 | #include "missing_audit.h" |
07630cea | 45 | #include "mkdir.h" |
6bedfcbb | 46 | #include "parse-util.h" |
b1852c48 | 47 | #include "path-util.h" |
4e731273 | 48 | #include "proc-cmdline.h" |
07630cea LP |
49 | #include "process-util.h" |
50 | #include "rm-rf.h" | |
51 | #include "selinux-util.h" | |
52 | #include "signal-util.h" | |
53 | #include "socket-util.h" | |
32917e33 | 54 | #include "stdio-util.h" |
8b43440b | 55 | #include "string-table.h" |
07630cea | 56 | #include "string-util.h" |
863a5610 | 57 | #include "syslog-util.h" |
98dcb8f4 | 58 | #include "user-record.h" |
22e3a02b | 59 | #include "user-util.h" |
d025f1e4 | 60 | |
d025f1e4 ZJS |
61 | #define USER_JOURNALS_MAX 1024 |
62 | ||
26687bf8 | 63 | #define DEFAULT_SYNC_INTERVAL_USEC (5*USEC_PER_MINUTE) |
7f1ad696 | 64 | #define DEFAULT_RATE_LIMIT_INTERVAL (30*USEC_PER_SEC) |
3de8ff5a | 65 | #define DEFAULT_RATE_LIMIT_BURST 10000 |
e150e820 | 66 | #define DEFAULT_MAX_FILE_USEC USEC_PER_MONTH |
d025f1e4 | 67 | |
8580d1f7 | 68 | #define RECHECK_SPACE_USEC (30*USEC_PER_SEC) |
d025f1e4 | 69 | |
e22aa3d3 LP |
70 | #define NOTIFY_SNDBUF_SIZE (8*1024*1024) |
71 | ||
7a24f3bf VC |
72 | /* The period to insert between posting changes for coalescing */ |
73 | #define POST_CHANGE_TIMER_INTERVAL_USEC (250*USEC_PER_MSEC) | |
74 | ||
ec20fe5f LP |
75 | /* Pick a good default that is likely to fit into AF_UNIX and AF_INET SOCK_DGRAM datagrams, and even leaves some room |
76 | * for a bit of additional metadata. */ | |
77 | #define DEFAULT_LINE_MAX (48*1024) | |
78 | ||
a33687b7 LP |
79 | #define DEFERRED_CLOSES_MAX (4096) |
80 | ||
65c398c0 LP |
81 | #define IDLE_TIMEOUT_USEC (30*USEC_PER_SEC) |
82 | ||
4f603103 LP |
83 | static int determine_path_usage( |
84 | Server *s, | |
85 | const char *path, | |
86 | uint64_t *ret_used, | |
87 | uint64_t *ret_free) { | |
88 | ||
e0ed6db9 FB |
89 | _cleanup_closedir_ DIR *d = NULL; |
90 | struct dirent *de; | |
91 | struct statvfs ss; | |
e0ed6db9 | 92 | |
4f603103 LP |
93 | assert(s); |
94 | assert(path); | |
e0ed6db9 FB |
95 | assert(ret_used); |
96 | assert(ret_free); | |
97 | ||
266a4700 | 98 | d = opendir(path); |
e0ed6db9 FB |
99 | if (!d) |
100 | return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, | |
266a4700 | 101 | errno, "Failed to open %s: %m", path); |
e0ed6db9 FB |
102 | |
103 | if (fstatvfs(dirfd(d), &ss) < 0) | |
266a4700 | 104 | return log_error_errno(errno, "Failed to fstatvfs(%s): %m", path); |
e0ed6db9 FB |
105 | |
106 | *ret_free = ss.f_bsize * ss.f_bavail; | |
107 | *ret_used = 0; | |
108 | FOREACH_DIRENT_ALL(de, d, break) { | |
109 | struct stat st; | |
110 | ||
111 | if (!endswith(de->d_name, ".journal") && | |
112 | !endswith(de->d_name, ".journal~")) | |
113 | continue; | |
114 | ||
115 | if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { | |
266a4700 | 116 | log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", path, de->d_name); |
e0ed6db9 FB |
117 | continue; |
118 | } | |
119 | ||
120 | if (!S_ISREG(st.st_mode)) | |
121 | continue; | |
122 | ||
123 | *ret_used += (uint64_t) st.st_blocks * 512UL; | |
124 | } | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
a0edc477 | 129 | static void cache_space_invalidate(JournalStorageSpace *space) { |
67319249 | 130 | zero(*space); |
a0edc477 FB |
131 | } |
132 | ||
57f443a6 | 133 | static int cache_space_refresh(Server *s, JournalStorage *storage) { |
23aba343 | 134 | JournalStorageSpace *space; |
266a4700 | 135 | JournalMetrics *metrics; |
23aba343 | 136 | uint64_t vfs_used, vfs_avail, avail; |
d025f1e4 | 137 | usec_t ts; |
e0ed6db9 | 138 | int r; |
d025f1e4 | 139 | |
8580d1f7 | 140 | assert(s); |
266a4700 | 141 | |
266a4700 | 142 | metrics = &storage->metrics; |
23aba343 | 143 | space = &storage->space; |
d025f1e4 | 144 | |
8580d1f7 | 145 | ts = now(CLOCK_MONOTONIC); |
d025f1e4 | 146 | |
3099caf2 | 147 | if (space->timestamp != 0 && space->timestamp + RECHECK_SPACE_USEC > ts) |
d025f1e4 ZJS |
148 | return 0; |
149 | ||
23aba343 | 150 | r = determine_path_usage(s, storage->path, &vfs_used, &vfs_avail); |
e0ed6db9 FB |
151 | if (r < 0) |
152 | return r; | |
d025f1e4 | 153 | |
23aba343 FB |
154 | space->vfs_used = vfs_used; |
155 | space->vfs_available = vfs_avail; | |
156 | ||
157 | avail = LESS_BY(vfs_avail, metrics->keep_free); | |
158 | ||
23aba343 FB |
159 | space->limit = MIN(MAX(vfs_used + avail, metrics->min_use), metrics->max_use); |
160 | space->available = LESS_BY(space->limit, vfs_used); | |
161 | space->timestamp = ts; | |
8580d1f7 LP |
162 | return 1; |
163 | } | |
164 | ||
3a19f215 FB |
165 | static void patch_min_use(JournalStorage *storage) { |
166 | assert(storage); | |
167 | ||
168 | /* Let's bump the min_use limit to the current usage on disk. We do | |
169 | * this when starting up and first opening the journal files. This way | |
170 | * sudden spikes in disk usage will not cause journald to vacuum files | |
171 | * without bounds. Note that this means that only a restart of journald | |
172 | * will make it reset this value. */ | |
173 | ||
174 | storage->metrics.min_use = MAX(storage->metrics.min_use, storage->space.vfs_used); | |
175 | } | |
176 | ||
b42b9479 LP |
177 | static JournalStorage* server_current_storage(Server *s) { |
178 | assert(s); | |
179 | ||
180 | return s->system_journal ? &s->system_storage : &s->runtime_storage; | |
181 | } | |
182 | ||
3a19f215 | 183 | static int determine_space(Server *s, uint64_t *available, uint64_t *limit) { |
266a4700 | 184 | JournalStorage *js; |
57f443a6 | 185 | int r; |
8580d1f7 LP |
186 | |
187 | assert(s); | |
188 | ||
b42b9479 | 189 | js = server_current_storage(s); |
57f443a6 FB |
190 | |
191 | r = cache_space_refresh(s, js); | |
192 | if (r >= 0) { | |
193 | if (available) | |
194 | *available = js->space.available; | |
195 | if (limit) | |
196 | *limit = js->space.limit; | |
197 | } | |
198 | return r; | |
d025f1e4 ZJS |
199 | } |
200 | ||
cba5629e FB |
201 | void server_space_usage_message(Server *s, JournalStorage *storage) { |
202 | char fb1[FORMAT_BYTES_MAX], fb2[FORMAT_BYTES_MAX], fb3[FORMAT_BYTES_MAX], | |
203 | fb4[FORMAT_BYTES_MAX], fb5[FORMAT_BYTES_MAX], fb6[FORMAT_BYTES_MAX]; | |
204 | JournalMetrics *metrics; | |
cba5629e FB |
205 | |
206 | assert(s); | |
207 | ||
208 | if (!storage) | |
b42b9479 | 209 | storage = server_current_storage(s); |
cba5629e | 210 | |
57f443a6 | 211 | if (cache_space_refresh(s, storage) < 0) |
cba5629e FB |
212 | return; |
213 | ||
214 | metrics = &storage->metrics; | |
23aba343 | 215 | format_bytes(fb1, sizeof(fb1), storage->space.vfs_used); |
cba5629e FB |
216 | format_bytes(fb2, sizeof(fb2), metrics->max_use); |
217 | format_bytes(fb3, sizeof(fb3), metrics->keep_free); | |
23aba343 | 218 | format_bytes(fb4, sizeof(fb4), storage->space.vfs_available); |
cba5629e FB |
219 | format_bytes(fb5, sizeof(fb5), storage->space.limit); |
220 | format_bytes(fb6, sizeof(fb6), storage->space.available); | |
221 | ||
13181942 LP |
222 | server_driver_message(s, 0, |
223 | "MESSAGE_ID=" SD_MESSAGE_JOURNAL_USAGE_STR, | |
cba5629e FB |
224 | LOG_MESSAGE("%s (%s) is %s, max %s, %s free.", |
225 | storage->name, storage->path, fb1, fb5, fb6), | |
226 | "JOURNAL_NAME=%s", storage->name, | |
227 | "JOURNAL_PATH=%s", storage->path, | |
23aba343 | 228 | "CURRENT_USE=%"PRIu64, storage->space.vfs_used, |
cba5629e FB |
229 | "CURRENT_USE_PRETTY=%s", fb1, |
230 | "MAX_USE=%"PRIu64, metrics->max_use, | |
231 | "MAX_USE_PRETTY=%s", fb2, | |
232 | "DISK_KEEP_FREE=%"PRIu64, metrics->keep_free, | |
233 | "DISK_KEEP_FREE_PRETTY=%s", fb3, | |
23aba343 | 234 | "DISK_AVAILABLE=%"PRIu64, storage->space.vfs_available, |
cba5629e FB |
235 | "DISK_AVAILABLE_PRETTY=%s", fb4, |
236 | "LIMIT=%"PRIu64, storage->space.limit, | |
237 | "LIMIT_PRETTY=%s", fb5, | |
238 | "AVAILABLE=%"PRIu64, storage->space.available, | |
239 | "AVAILABLE_PRETTY=%s", fb6, | |
240 | NULL); | |
241 | } | |
242 | ||
2fce06b0 LP |
243 | static bool uid_for_system_journal(uid_t uid) { |
244 | ||
245 | /* Returns true if the specified UID shall get its data stored in the system journal*/ | |
246 | ||
247 | return uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY; | |
248 | } | |
249 | ||
5c3bde3f | 250 | static void server_add_acls(JournalFile *f, uid_t uid) { |
d025f1e4 ZJS |
251 | assert(f); |
252 | ||
349cc4a5 | 253 | #if HAVE_ACL |
567aeb58 ZJS |
254 | int r; |
255 | ||
2fce06b0 | 256 | if (uid_for_system_journal(uid)) |
d025f1e4 ZJS |
257 | return; |
258 | ||
567aeb58 | 259 | r = fd_add_uid_acl_permission(f->fd, uid, ACL_READ); |
5c3bde3f ZJS |
260 | if (r < 0) |
261 | log_warning_errno(r, "Failed to set ACL on %s, ignoring: %m", f->path); | |
d025f1e4 ZJS |
262 | #endif |
263 | } | |
264 | ||
7a24f3bf VC |
265 | static int open_journal( |
266 | Server *s, | |
267 | bool reliably, | |
268 | const char *fname, | |
269 | int flags, | |
270 | bool seal, | |
271 | JournalMetrics *metrics, | |
7a24f3bf | 272 | JournalFile **ret) { |
e8591544 | 273 | |
627df1dc | 274 | _cleanup_(journal_file_closep) JournalFile *f = NULL; |
e8591544 | 275 | int r; |
7a24f3bf VC |
276 | |
277 | assert(s); | |
278 | assert(fname); | |
279 | assert(ret); | |
280 | ||
281 | if (reliably) | |
1b7cf0e5 AG |
282 | r = journal_file_open_reliably(fname, flags, 0640, s->compress.enabled, s->compress.threshold_bytes, |
283 | seal, metrics, s->mmap, s->deferred_closes, NULL, &f); | |
7a24f3bf | 284 | else |
1b7cf0e5 AG |
285 | r = journal_file_open(-1, fname, flags, 0640, s->compress.enabled, s->compress.threshold_bytes, seal, |
286 | metrics, s->mmap, s->deferred_closes, NULL, &f); | |
287 | ||
7a24f3bf VC |
288 | if (r < 0) |
289 | return r; | |
290 | ||
e167d7fd | 291 | r = journal_file_enable_post_change_timer(f, s->event, POST_CHANGE_TIMER_INTERVAL_USEC); |
627df1dc | 292 | if (r < 0) |
7a24f3bf | 293 | return r; |
7a24f3bf | 294 | |
627df1dc | 295 | *ret = TAKE_PTR(f); |
7a24f3bf VC |
296 | return r; |
297 | } | |
298 | ||
b1852c48 LP |
299 | static bool flushed_flag_is_set(Server *s) { |
300 | const char *fn; | |
301 | ||
302 | assert(s); | |
303 | ||
304 | /* We don't support the "flushing" concept for namespace instances, we assume them to always have | |
305 | * access to /var */ | |
306 | if (s->namespace) | |
307 | return true; | |
308 | ||
309 | fn = strjoina(s->runtime_directory, "/flushed"); | |
310 | return access(fn, F_OK) >= 0; | |
6431c7e2 VC |
311 | } |
312 | ||
b4e26d1d | 313 | static int system_journal_open(Server *s, bool flush_requested, bool relinquish_requested) { |
105bdb46 VC |
314 | const char *fn; |
315 | int r = 0; | |
316 | ||
317 | if (!s->system_journal && | |
f78273c8 | 318 | IN_SET(s->storage, STORAGE_PERSISTENT, STORAGE_AUTO) && |
b1852c48 | 319 | (flush_requested || flushed_flag_is_set(s)) && |
b4e26d1d | 320 | !relinquish_requested) { |
105bdb46 | 321 | |
d6f46470 | 322 | /* If in auto mode: first try to create the machine path, but not the prefix. |
105bdb46 | 323 | * |
d6f46470 | 324 | * If in persistent mode: create /var/log/journal and the machine path */ |
105bdb46 VC |
325 | |
326 | if (s->storage == STORAGE_PERSISTENT) | |
d6f46470 | 327 | (void) mkdir_parents(s->system_storage.path, 0755); |
105bdb46 | 328 | |
266a4700 | 329 | (void) mkdir(s->system_storage.path, 0755); |
105bdb46 | 330 | |
266a4700 FB |
331 | fn = strjoina(s->system_storage.path, "/system.journal"); |
332 | r = open_journal(s, true, fn, O_RDWR|O_CREAT, s->seal, &s->system_storage.metrics, &s->system_journal); | |
105bdb46 VC |
333 | if (r >= 0) { |
334 | server_add_acls(s->system_journal, 0); | |
57f443a6 | 335 | (void) cache_space_refresh(s, &s->system_storage); |
3a19f215 | 336 | patch_min_use(&s->system_storage); |
29bfb683 | 337 | } else { |
4c701096 | 338 | if (!IN_SET(r, -ENOENT, -EROFS)) |
105bdb46 VC |
339 | log_warning_errno(r, "Failed to open system journal: %m"); |
340 | ||
341 | r = 0; | |
342 | } | |
929eeb54 | 343 | |
d6f46470 LP |
344 | /* If the runtime journal is open, and we're post-flush, we're recovering from a failed |
345 | * system journal rotate (ENOSPC) for which the runtime journal was reopened. | |
929eeb54 | 346 | * |
d6f46470 LP |
347 | * Perform an implicit flush to var, leaving the runtime journal closed, now that the system |
348 | * journal is back. | |
929eeb54 | 349 | */ |
f78273c8 LP |
350 | if (!flush_requested) |
351 | (void) server_flush_to_var(s, true); | |
105bdb46 VC |
352 | } |
353 | ||
354 | if (!s->runtime_journal && | |
355 | (s->storage != STORAGE_NONE)) { | |
356 | ||
266a4700 | 357 | fn = strjoina(s->runtime_storage.path, "/system.journal"); |
105bdb46 | 358 | |
b4e26d1d | 359 | if (s->system_journal && !relinquish_requested) { |
105bdb46 VC |
360 | |
361 | /* Try to open the runtime journal, but only | |
362 | * if it already exists, so that we can flush | |
363 | * it into the system journal */ | |
364 | ||
266a4700 | 365 | r = open_journal(s, false, fn, O_RDWR, false, &s->runtime_storage.metrics, &s->runtime_journal); |
105bdb46 VC |
366 | if (r < 0) { |
367 | if (r != -ENOENT) | |
368 | log_warning_errno(r, "Failed to open runtime journal: %m"); | |
369 | ||
370 | r = 0; | |
371 | } | |
372 | ||
373 | } else { | |
374 | ||
d6f46470 | 375 | /* OK, we really need the runtime journal, so create it if necessary. */ |
105bdb46 | 376 | |
d6f46470 LP |
377 | (void) mkdir_parents(s->runtime_storage.path, 0755); |
378 | (void) mkdir(s->runtime_storage.path, 0750); | |
105bdb46 | 379 | |
266a4700 | 380 | r = open_journal(s, true, fn, O_RDWR|O_CREAT, false, &s->runtime_storage.metrics, &s->runtime_journal); |
105bdb46 VC |
381 | if (r < 0) |
382 | return log_error_errno(r, "Failed to open runtime journal: %m"); | |
383 | } | |
384 | ||
385 | if (s->runtime_journal) { | |
386 | server_add_acls(s->runtime_journal, 0); | |
57f443a6 | 387 | (void) cache_space_refresh(s, &s->runtime_storage); |
3a19f215 | 388 | patch_min_use(&s->runtime_storage); |
105bdb46 VC |
389 | } |
390 | } | |
391 | ||
392 | return r; | |
393 | } | |
394 | ||
d025f1e4 | 395 | static JournalFile* find_journal(Server *s, uid_t uid) { |
ed375beb | 396 | _cleanup_free_ char *p = NULL; |
d025f1e4 | 397 | JournalFile *f; |
46e2348a | 398 | int r; |
d025f1e4 ZJS |
399 | |
400 | assert(s); | |
401 | ||
46e2348a LP |
402 | /* A rotate that fails to create the new journal (ENOSPC) leaves the rotated journal as NULL. Unless |
403 | * we revisit opening, even after space is made available we'll continue to return NULL indefinitely. | |
105bdb46 | 404 | * |
46e2348a LP |
405 | * system_journal_open() is a noop if the journals are already open, so we can just call it here to |
406 | * recover from failed rotates (or anything else that's left the journals as NULL). | |
105bdb46 VC |
407 | * |
408 | * Fixes https://github.com/systemd/systemd/issues/3968 */ | |
b4e26d1d | 409 | (void) system_journal_open(s, false, false); |
105bdb46 | 410 | |
46e2348a LP |
411 | /* We split up user logs only on /var, not on /run. If the runtime file is open, we write to it |
412 | * exclusively, in order to guarantee proper order as soon as we flush /run to /var and close the | |
413 | * runtime file. */ | |
d025f1e4 ZJS |
414 | |
415 | if (s->runtime_journal) | |
416 | return s->runtime_journal; | |
417 | ||
2fce06b0 | 418 | if (uid_for_system_journal(uid)) |
d025f1e4 ZJS |
419 | return s->system_journal; |
420 | ||
4a0b58c4 | 421 | f = ordered_hashmap_get(s->user_journals, UID_TO_PTR(uid)); |
d025f1e4 ZJS |
422 | if (f) |
423 | return f; | |
424 | ||
46e2348a | 425 | if (asprintf(&p, "%s/user-" UID_FMT ".journal", s->system_storage.path, uid) < 0) { |
d0307775 | 426 | log_oom(); |
d025f1e4 | 427 | return s->system_journal; |
d0307775 | 428 | } |
d025f1e4 | 429 | |
46e2348a | 430 | /* Too many open? Then let's close one (or more) */ |
43cf8388 | 431 | while (ordered_hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) { |
46e2348a | 432 | assert_se(f = ordered_hashmap_steal_first(s->user_journals)); |
69a3a6fd | 433 | (void) journal_file_close(f); |
d025f1e4 ZJS |
434 | } |
435 | ||
266a4700 | 436 | r = open_journal(s, true, p, O_RDWR|O_CREAT, s->seal, &s->system_storage.metrics, &f); |
d025f1e4 ZJS |
437 | if (r < 0) |
438 | return s->system_journal; | |
439 | ||
4a0b58c4 | 440 | r = ordered_hashmap_put(s->user_journals, UID_TO_PTR(uid), f); |
d025f1e4 | 441 | if (r < 0) { |
69a3a6fd | 442 | (void) journal_file_close(f); |
d025f1e4 ZJS |
443 | return s->system_journal; |
444 | } | |
445 | ||
46e2348a | 446 | server_add_acls(f, uid); |
d025f1e4 ZJS |
447 | return f; |
448 | } | |
449 | ||
ea69bd41 LP |
450 | static int do_rotate( |
451 | Server *s, | |
452 | JournalFile **f, | |
453 | const char* name, | |
454 | bool seal, | |
455 | uint32_t uid) { | |
456 | ||
fc55baee ZJS |
457 | int r; |
458 | assert(s); | |
459 | ||
460 | if (!*f) | |
461 | return -EINVAL; | |
462 | ||
1b7cf0e5 | 463 | r = journal_file_rotate(f, s->compress.enabled, s->compress.threshold_bytes, seal, s->deferred_closes); |
bb6b922f | 464 | if (r < 0) { |
fc55baee | 465 | if (*f) |
bb6b922f | 466 | return log_error_errno(r, "Failed to rotate %s: %m", (*f)->path); |
fc55baee | 467 | else |
bb6b922f YW |
468 | return log_error_errno(r, "Failed to create new %s journal: %m", name); |
469 | } | |
470 | ||
471 | server_add_acls(*f, uid); | |
fc55baee ZJS |
472 | return r; |
473 | } | |
474 | ||
f760d8a8 LP |
475 | static void server_process_deferred_closes(Server *s) { |
476 | JournalFile *f; | |
f760d8a8 LP |
477 | |
478 | /* Perform any deferred closes which aren't still offlining. */ | |
90e74a66 | 479 | SET_FOREACH(f, s->deferred_closes) { |
a33687b7 LP |
480 | if (journal_file_is_offlining(f)) |
481 | continue; | |
482 | ||
483 | (void) set_remove(s->deferred_closes, f); | |
484 | (void) journal_file_close(f); | |
485 | } | |
486 | } | |
487 | ||
488 | static void server_vacuum_deferred_closes(Server *s) { | |
489 | assert(s); | |
490 | ||
491 | /* Make some room in the deferred closes list, so that it doesn't grow without bounds */ | |
492 | if (set_size(s->deferred_closes) < DEFERRED_CLOSES_MAX) | |
493 | return; | |
494 | ||
495 | /* Let's first remove all journal files that might already have completed closing */ | |
496 | server_process_deferred_closes(s); | |
497 | ||
498 | /* And now, let's close some more until we reach the limit again. */ | |
499 | while (set_size(s->deferred_closes) >= DEFERRED_CLOSES_MAX) { | |
500 | JournalFile *f; | |
501 | ||
502 | assert_se(f = set_steal_first(s->deferred_closes)); | |
503 | journal_file_close(f); | |
504 | } | |
505 | } | |
506 | ||
4e00337b LP |
507 | static int vacuum_offline_user_journals(Server *s) { |
508 | _cleanup_closedir_ DIR *d = NULL; | |
a33687b7 LP |
509 | int r; |
510 | ||
511 | assert(s); | |
512 | ||
4e00337b LP |
513 | d = opendir(s->system_storage.path); |
514 | if (!d) { | |
515 | if (errno == ENOENT) | |
516 | return 0; | |
517 | ||
518 | return log_error_errno(errno, "Failed to open %s: %m", s->system_storage.path); | |
519 | } | |
520 | ||
521 | for (;;) { | |
522 | _cleanup_free_ char *u = NULL, *full = NULL; | |
523 | _cleanup_close_ int fd = -1; | |
524 | const char *a, *b; | |
525 | struct dirent *de; | |
526 | JournalFile *f; | |
527 | uid_t uid; | |
528 | ||
529 | errno = 0; | |
530 | de = readdir_no_dot(d); | |
531 | if (!de) { | |
532 | if (errno != 0) | |
533 | log_warning_errno(errno, "Failed to enumerate %s, ignoring: %m", s->system_storage.path); | |
534 | ||
535 | break; | |
536 | } | |
537 | ||
538 | a = startswith(de->d_name, "user-"); | |
539 | if (!a) | |
540 | continue; | |
541 | b = endswith(de->d_name, ".journal"); | |
542 | if (!b) | |
543 | continue; | |
544 | ||
545 | u = strndup(a, b-a); | |
546 | if (!u) | |
547 | return log_oom(); | |
a33687b7 | 548 | |
4e00337b LP |
549 | r = parse_uid(u, &uid); |
550 | if (r < 0) { | |
551 | log_debug_errno(r, "Failed to parse UID from file name '%s', ignoring: %m", de->d_name); | |
552 | continue; | |
553 | } | |
554 | ||
555 | /* Already rotated in the above loop? i.e. is it an open user journal? */ | |
556 | if (ordered_hashmap_contains(s->user_journals, UID_TO_PTR(uid))) | |
557 | continue; | |
558 | ||
559 | full = path_join(s->system_storage.path, de->d_name); | |
560 | if (!full) | |
561 | return log_oom(); | |
562 | ||
563 | fd = openat(dirfd(d), de->d_name, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK); | |
564 | if (fd < 0) { | |
565 | log_full_errno(IN_SET(errno, ELOOP, ENOENT) ? LOG_DEBUG : LOG_WARNING, errno, | |
566 | "Failed to open journal file '%s' for rotation: %m", full); | |
567 | continue; | |
568 | } | |
a33687b7 | 569 | |
4e00337b LP |
570 | /* Make some room in the set of deferred close()s */ |
571 | server_vacuum_deferred_closes(s); | |
572 | ||
573 | /* Open the file briefly, so that we can archive it */ | |
574 | r = journal_file_open(fd, | |
575 | full, | |
576 | O_RDWR, | |
577 | 0640, | |
578 | s->compress.enabled, | |
579 | s->compress.threshold_bytes, | |
580 | s->seal, | |
581 | &s->system_storage.metrics, | |
582 | s->mmap, | |
583 | s->deferred_closes, | |
584 | NULL, | |
585 | &f); | |
586 | if (r < 0) { | |
587 | log_warning_errno(r, "Failed to read journal file %s for rotation, trying to move it out of the way: %m", full); | |
588 | ||
589 | r = journal_file_dispose(dirfd(d), de->d_name); | |
590 | if (r < 0) | |
591 | log_warning_errno(r, "Failed to move %s out of the way, ignoring: %m", full); | |
592 | else | |
593 | log_debug("Successfully moved %s out of the way.", full); | |
a33687b7 | 594 | |
4e00337b LP |
595 | continue; |
596 | } | |
597 | ||
598 | TAKE_FD(fd); /* Donated to journal_file_open() */ | |
599 | ||
600 | r = journal_file_archive(f); | |
601 | if (r < 0) | |
602 | log_debug_errno(r, "Failed to archive journal file '%s', ignoring: %m", full); | |
603 | ||
604 | f = journal_initiate_close(f, s->deferred_closes); | |
605 | } | |
a33687b7 LP |
606 | |
607 | return 0; | |
f760d8a8 LP |
608 | } |
609 | ||
d025f1e4 ZJS |
610 | void server_rotate(Server *s) { |
611 | JournalFile *f; | |
a33687b7 | 612 | void *k; |
d025f1e4 ZJS |
613 | int r; |
614 | ||
615 | log_debug("Rotating..."); | |
616 | ||
a33687b7 | 617 | /* First, rotate the system journal (either in its runtime flavour or in its runtime flavour) */ |
8580d1f7 LP |
618 | (void) do_rotate(s, &s->runtime_journal, "runtime", false, 0); |
619 | (void) do_rotate(s, &s->system_journal, "system", s->seal, 0); | |
d025f1e4 | 620 | |
a33687b7 | 621 | /* Then, rotate all user journals we have open (keeping them open) */ |
90e74a66 | 622 | ORDERED_HASHMAP_FOREACH_KEY(f, k, s->user_journals) { |
4a0b58c4 | 623 | r = do_rotate(s, &f, "user", s->seal, PTR_TO_UID(k)); |
fc55baee | 624 | if (r >= 0) |
43cf8388 | 625 | ordered_hashmap_replace(s->user_journals, k, f); |
fc55baee ZJS |
626 | else if (!f) |
627 | /* Old file has been closed and deallocated */ | |
43cf8388 | 628 | ordered_hashmap_remove(s->user_journals, k); |
d025f1e4 | 629 | } |
b58c888f | 630 | |
4e00337b LP |
631 | /* Finally, also rotate all user journals we currently do not have open. (But do so only if we |
632 | * actually have access to /var, i.e. are not in the log-to-runtime-journal mode). */ | |
633 | if (!s->runtime_journal) | |
634 | (void) vacuum_offline_user_journals(s); | |
a33687b7 | 635 | |
f760d8a8 | 636 | server_process_deferred_closes(s); |
d025f1e4 ZJS |
637 | } |
638 | ||
26687bf8 OS |
639 | void server_sync(Server *s) { |
640 | JournalFile *f; | |
26687bf8 OS |
641 | int r; |
642 | ||
26687bf8 | 643 | if (s->system_journal) { |
ac2e41f5 | 644 | r = journal_file_set_offline(s->system_journal, false); |
26687bf8 | 645 | if (r < 0) |
65089b82 | 646 | log_warning_errno(r, "Failed to sync system journal, ignoring: %m"); |
26687bf8 OS |
647 | } |
648 | ||
90e74a66 | 649 | ORDERED_HASHMAP_FOREACH(f, s->user_journals) { |
ac2e41f5 | 650 | r = journal_file_set_offline(f, false); |
26687bf8 | 651 | if (r < 0) |
65089b82 | 652 | log_warning_errno(r, "Failed to sync user journal, ignoring: %m"); |
26687bf8 OS |
653 | } |
654 | ||
f9a810be LP |
655 | if (s->sync_event_source) { |
656 | r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_OFF); | |
657 | if (r < 0) | |
da927ba9 | 658 | log_error_errno(r, "Failed to disable sync timer source: %m"); |
f9a810be | 659 | } |
26687bf8 OS |
660 | |
661 | s->sync_scheduled = false; | |
662 | } | |
663 | ||
3a19f215 | 664 | static void do_vacuum(Server *s, JournalStorage *storage, bool verbose) { |
ea69bd41 | 665 | |
63c8666b ZJS |
666 | int r; |
667 | ||
8580d1f7 | 668 | assert(s); |
266a4700 | 669 | assert(storage); |
8580d1f7 | 670 | |
57f443a6 | 671 | (void) cache_space_refresh(s, storage); |
18e758bf FB |
672 | |
673 | if (verbose) | |
674 | server_space_usage_message(s, storage); | |
8580d1f7 | 675 | |
57f443a6 FB |
676 | r = journal_directory_vacuum(storage->path, storage->space.limit, |
677 | storage->metrics.n_max_files, s->max_retention_usec, | |
678 | &s->oldest_file_usec, verbose); | |
63c8666b | 679 | if (r < 0 && r != -ENOENT) |
266a4700 FB |
680 | log_warning_errno(r, "Failed to vacuum %s, ignoring: %m", storage->path); |
681 | ||
a0edc477 | 682 | cache_space_invalidate(&storage->space); |
63c8666b ZJS |
683 | } |
684 | ||
3a19f215 | 685 | int server_vacuum(Server *s, bool verbose) { |
8580d1f7 | 686 | assert(s); |
d025f1e4 ZJS |
687 | |
688 | log_debug("Vacuuming..."); | |
689 | ||
690 | s->oldest_file_usec = 0; | |
691 | ||
266a4700 | 692 | if (s->system_journal) |
3a19f215 | 693 | do_vacuum(s, &s->system_storage, verbose); |
266a4700 | 694 | if (s->runtime_journal) |
3a19f215 | 695 | do_vacuum(s, &s->runtime_storage, verbose); |
d025f1e4 | 696 | |
8580d1f7 | 697 | return 0; |
d025f1e4 ZJS |
698 | } |
699 | ||
0c24bb23 LP |
700 | static void server_cache_machine_id(Server *s) { |
701 | sd_id128_t id; | |
702 | int r; | |
703 | ||
704 | assert(s); | |
705 | ||
706 | r = sd_id128_get_machine(&id); | |
707 | if (r < 0) | |
708 | return; | |
709 | ||
710 | sd_id128_to_string(id, stpcpy(s->machine_id_field, "_MACHINE_ID=")); | |
711 | } | |
712 | ||
713 | static void server_cache_boot_id(Server *s) { | |
714 | sd_id128_t id; | |
715 | int r; | |
716 | ||
717 | assert(s); | |
718 | ||
719 | r = sd_id128_get_boot(&id); | |
720 | if (r < 0) | |
721 | return; | |
722 | ||
723 | sd_id128_to_string(id, stpcpy(s->boot_id_field, "_BOOT_ID=")); | |
724 | } | |
725 | ||
726 | static void server_cache_hostname(Server *s) { | |
727 | _cleanup_free_ char *t = NULL; | |
728 | char *x; | |
729 | ||
730 | assert(s); | |
731 | ||
732 | t = gethostname_malloc(); | |
733 | if (!t) | |
734 | return; | |
735 | ||
b910cc72 | 736 | x = strjoin("_HOSTNAME=", t); |
0c24bb23 LP |
737 | if (!x) |
738 | return; | |
739 | ||
99d0d05a | 740 | free_and_replace(s->hostname_field, x); |
0c24bb23 LP |
741 | } |
742 | ||
8531ae70 | 743 | static bool shall_try_append_again(JournalFile *f, int r) { |
6e1045e5 | 744 | switch(r) { |
ae739cc1 | 745 | |
6e1045e5 ZJS |
746 | case -E2BIG: /* Hit configured limit */ |
747 | case -EFBIG: /* Hit fs limit */ | |
748 | case -EDQUOT: /* Quota limit hit */ | |
749 | case -ENOSPC: /* Disk full */ | |
d025f1e4 | 750 | log_debug("%s: Allocation limit reached, rotating.", f->path); |
6e1045e5 | 751 | return true; |
ae739cc1 | 752 | |
6e1045e5 ZJS |
753 | case -EIO: /* I/O error of some kind (mmap) */ |
754 | log_warning("%s: IO error, rotating.", f->path); | |
755 | return true; | |
ae739cc1 | 756 | |
6e1045e5 | 757 | case -EHOSTDOWN: /* Other machine */ |
d025f1e4 | 758 | log_info("%s: Journal file from other machine, rotating.", f->path); |
6e1045e5 | 759 | return true; |
ae739cc1 | 760 | |
6e1045e5 | 761 | case -EBUSY: /* Unclean shutdown */ |
d025f1e4 | 762 | log_info("%s: Unclean shutdown, rotating.", f->path); |
6e1045e5 | 763 | return true; |
ae739cc1 | 764 | |
6e1045e5 | 765 | case -EPROTONOSUPPORT: /* Unsupported feature */ |
d025f1e4 | 766 | log_info("%s: Unsupported feature, rotating.", f->path); |
6e1045e5 | 767 | return true; |
ae739cc1 | 768 | |
6e1045e5 ZJS |
769 | case -EBADMSG: /* Corrupted */ |
770 | case -ENODATA: /* Truncated */ | |
771 | case -ESHUTDOWN: /* Already archived */ | |
d025f1e4 | 772 | log_warning("%s: Journal file corrupted, rotating.", f->path); |
6e1045e5 | 773 | return true; |
ae739cc1 | 774 | |
6e1045e5 | 775 | case -EIDRM: /* Journal file has been deleted */ |
2678031a | 776 | log_warning("%s: Journal file has been deleted, rotating.", f->path); |
6e1045e5 | 777 | return true; |
ae739cc1 LP |
778 | |
779 | case -ETXTBSY: /* Journal file is from the future */ | |
c1a9199e | 780 | log_warning("%s: Journal file is from the future, rotating.", f->path); |
ae739cc1 LP |
781 | return true; |
782 | ||
5087825e LP |
783 | case -EAFNOSUPPORT: |
784 | log_warning("%s: underlying file system does not support memory mapping or another required file system feature.", f->path); | |
785 | return false; | |
786 | ||
6e1045e5 | 787 | default: |
d025f1e4 | 788 | return false; |
6e1045e5 | 789 | } |
d025f1e4 ZJS |
790 | } |
791 | ||
da6053d0 | 792 | static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, size_t n, int priority) { |
7c070017 | 793 | bool vacuumed = false, rotate = false; |
0f972d66 | 794 | struct dual_timestamp ts; |
d025f1e4 | 795 | JournalFile *f; |
d025f1e4 ZJS |
796 | int r; |
797 | ||
798 | assert(s); | |
799 | assert(iovec); | |
800 | assert(n > 0); | |
801 | ||
0f972d66 LP |
802 | /* Get the closest, linearized time we have for this log event from the event loop. (Note that we do not use |
803 | * the source time, and not even the time the event was originally seen, but instead simply the time we started | |
804 | * processing it, as we want strictly linear ordering in what we write out.) */ | |
805 | assert_se(sd_event_now(s->event, CLOCK_REALTIME, &ts.realtime) >= 0); | |
806 | assert_se(sd_event_now(s->event, CLOCK_MONOTONIC, &ts.monotonic) >= 0); | |
807 | ||
7c070017 LP |
808 | if (ts.realtime < s->last_realtime_clock) { |
809 | /* When the time jumps backwards, let's immediately rotate. Of course, this should not happen during | |
810 | * regular operation. However, when it does happen, then we should make sure that we start fresh files | |
811 | * to ensure that the entries in the journal files are strictly ordered by time, in order to ensure | |
812 | * bisection works correctly. */ | |
d025f1e4 | 813 | |
7c070017 LP |
814 | log_debug("Time jumped backwards, rotating."); |
815 | rotate = true; | |
816 | } else { | |
817 | ||
818 | f = find_journal(s, uid); | |
819 | if (!f) | |
820 | return; | |
821 | ||
822 | if (journal_file_rotate_suggested(f, s->max_file_usec)) { | |
823 | log_debug("%s: Journal header limits reached or header out-of-date, rotating.", f->path); | |
824 | rotate = true; | |
825 | } | |
826 | } | |
d025f1e4 | 827 | |
7c070017 | 828 | if (rotate) { |
d025f1e4 | 829 | server_rotate(s); |
3a19f215 | 830 | server_vacuum(s, false); |
d025f1e4 ZJS |
831 | vacuumed = true; |
832 | ||
833 | f = find_journal(s, uid); | |
834 | if (!f) | |
835 | return; | |
836 | } | |
837 | ||
7c070017 LP |
838 | s->last_realtime_clock = ts.realtime; |
839 | ||
d180c349 | 840 | r = journal_file_append_entry(f, &ts, NULL, iovec, n, &s->seqnum, NULL, NULL); |
26687bf8 | 841 | if (r >= 0) { |
d07f7b9e | 842 | server_schedule_sync(s, priority); |
d025f1e4 | 843 | return; |
26687bf8 | 844 | } |
d025f1e4 ZJS |
845 | |
846 | if (vacuumed || !shall_try_append_again(f, r)) { | |
da6053d0 | 847 | log_error_errno(r, "Failed to write entry (%zu items, %zu bytes), ignoring: %m", n, IOVEC_TOTAL_SIZE(iovec, n)); |
d025f1e4 ZJS |
848 | return; |
849 | } | |
850 | ||
851 | server_rotate(s); | |
3a19f215 | 852 | server_vacuum(s, false); |
d025f1e4 ZJS |
853 | |
854 | f = find_journal(s, uid); | |
855 | if (!f) | |
856 | return; | |
857 | ||
858 | log_debug("Retrying write."); | |
d180c349 | 859 | r = journal_file_append_entry(f, &ts, NULL, iovec, n, &s->seqnum, NULL, NULL); |
8266e1c0 | 860 | if (r < 0) |
da6053d0 | 861 | log_error_errno(r, "Failed to write entry (%zu items, %zu bytes) despite vacuuming, ignoring: %m", n, IOVEC_TOTAL_SIZE(iovec, n)); |
8266e1c0 | 862 | else |
d07f7b9e | 863 | server_schedule_sync(s, priority); |
d025f1e4 ZJS |
864 | } |
865 | ||
22e3a02b LP |
866 | #define IOVEC_ADD_NUMERIC_FIELD(iovec, n, value, type, isset, format, field) \ |
867 | if (isset(value)) { \ | |
868 | char *k; \ | |
fbd0b64f | 869 | k = newa(char, STRLEN(field "=") + DECIMAL_STR_MAX(type) + 1); \ |
22e3a02b | 870 | sprintf(k, field "=" format, value); \ |
e6a7ec4b | 871 | iovec[n++] = IOVEC_MAKE_STRING(k); \ |
22e3a02b | 872 | } |
4b58153d | 873 | |
22e3a02b LP |
874 | #define IOVEC_ADD_STRING_FIELD(iovec, n, value, field) \ |
875 | if (!isempty(value)) { \ | |
876 | char *k; \ | |
877 | k = strjoina(field "=", value); \ | |
e6a7ec4b | 878 | iovec[n++] = IOVEC_MAKE_STRING(k); \ |
22e3a02b | 879 | } |
4b58153d | 880 | |
22e3a02b LP |
881 | #define IOVEC_ADD_ID128_FIELD(iovec, n, value, field) \ |
882 | if (!sd_id128_is_null(value)) { \ | |
883 | char *k; \ | |
fbd0b64f | 884 | k = newa(char, STRLEN(field "=") + SD_ID128_STRING_MAX); \ |
22e3a02b | 885 | sd_id128_to_string(value, stpcpy(k, field "=")); \ |
e6a7ec4b | 886 | iovec[n++] = IOVEC_MAKE_STRING(k); \ |
22e3a02b | 887 | } |
4b58153d | 888 | |
22e3a02b LP |
889 | #define IOVEC_ADD_SIZED_FIELD(iovec, n, value, value_size, field) \ |
890 | if (value_size > 0) { \ | |
891 | char *k; \ | |
fbd0b64f | 892 | k = newa(char, STRLEN(field "=") + value_size + 1); \ |
22e3a02b | 893 | *((char*) mempcpy(stpcpy(k, field "="), value, value_size)) = 0; \ |
e6a7ec4b | 894 | iovec[n++] = IOVEC_MAKE_STRING(k); \ |
22e3a02b | 895 | } \ |
4b58153d | 896 | |
d025f1e4 ZJS |
897 | static void dispatch_message_real( |
898 | Server *s, | |
d3070fbd | 899 | struct iovec *iovec, size_t n, size_t m, |
22e3a02b | 900 | const ClientContext *c, |
3b3154df | 901 | const struct timeval *tv, |
d07f7b9e | 902 | int priority, |
22e3a02b LP |
903 | pid_t object_pid) { |
904 | ||
905 | char source_time[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)]; | |
084eeb86 | 906 | _cleanup_free_ char *cmdline1 = NULL, *cmdline2 = NULL; |
22e3a02b LP |
907 | uid_t journal_uid; |
908 | ClientContext *o; | |
d025f1e4 ZJS |
909 | |
910 | assert(s); | |
911 | assert(iovec); | |
912 | assert(n > 0); | |
d3070fbd LP |
913 | assert(n + |
914 | N_IOVEC_META_FIELDS + | |
915 | (pid_is_valid(object_pid) ? N_IOVEC_OBJECT_FIELDS : 0) + | |
916 | client_context_extra_fields_n_iovec(c) <= m); | |
19cace37 | 917 | |
22e3a02b LP |
918 | if (c) { |
919 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->pid, pid_t, pid_is_valid, PID_FMT, "_PID"); | |
920 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->uid, uid_t, uid_is_valid, UID_FMT, "_UID"); | |
921 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->gid, gid_t, gid_is_valid, GID_FMT, "_GID"); | |
4b58153d | 922 | |
084eeb86 ZJS |
923 | IOVEC_ADD_STRING_FIELD(iovec, n, c->comm, "_COMM"); /* At most TASK_COMM_LENGTH (16 bytes) */ |
924 | IOVEC_ADD_STRING_FIELD(iovec, n, c->exe, "_EXE"); /* A path, so at most PATH_MAX (4096 bytes) */ | |
d025f1e4 | 925 | |
084eeb86 ZJS |
926 | if (c->cmdline) |
927 | /* At most _SC_ARG_MAX (2MB usually), which is too much to put on stack. | |
928 | * Let's use a heap allocation for this one. */ | |
929 | cmdline1 = set_iovec_string_field(iovec, &n, "_CMDLINE=", c->cmdline); | |
ae018d9b | 930 | |
084eeb86 ZJS |
931 | IOVEC_ADD_STRING_FIELD(iovec, n, c->capeff, "_CAP_EFFECTIVE"); /* Read from /proc/.../status */ |
932 | IOVEC_ADD_SIZED_FIELD(iovec, n, c->label, c->label_size, "_SELINUX_CONTEXT"); | |
22e3a02b LP |
933 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->auditid, uint32_t, audit_session_is_valid, "%" PRIu32, "_AUDIT_SESSION"); |
934 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->loginuid, uid_t, uid_is_valid, UID_FMT, "_AUDIT_LOGINUID"); | |
d025f1e4 | 935 | |
084eeb86 | 936 | IOVEC_ADD_STRING_FIELD(iovec, n, c->cgroup, "_SYSTEMD_CGROUP"); /* A path */ |
22e3a02b LP |
937 | IOVEC_ADD_STRING_FIELD(iovec, n, c->session, "_SYSTEMD_SESSION"); |
938 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->owner_uid, uid_t, uid_is_valid, UID_FMT, "_SYSTEMD_OWNER_UID"); | |
084eeb86 | 939 | IOVEC_ADD_STRING_FIELD(iovec, n, c->unit, "_SYSTEMD_UNIT"); /* Unit names are bounded by UNIT_NAME_MAX */ |
22e3a02b LP |
940 | IOVEC_ADD_STRING_FIELD(iovec, n, c->user_unit, "_SYSTEMD_USER_UNIT"); |
941 | IOVEC_ADD_STRING_FIELD(iovec, n, c->slice, "_SYSTEMD_SLICE"); | |
942 | IOVEC_ADD_STRING_FIELD(iovec, n, c->user_slice, "_SYSTEMD_USER_SLICE"); | |
e7ff4e7f | 943 | |
22e3a02b | 944 | IOVEC_ADD_ID128_FIELD(iovec, n, c->invocation_id, "_SYSTEMD_INVOCATION_ID"); |
d3070fbd LP |
945 | |
946 | if (c->extra_fields_n_iovec > 0) { | |
947 | memcpy(iovec + n, c->extra_fields_iovec, c->extra_fields_n_iovec * sizeof(struct iovec)); | |
948 | n += c->extra_fields_n_iovec; | |
949 | } | |
d025f1e4 | 950 | } |
968f3196 | 951 | |
22e3a02b | 952 | assert(n <= m); |
968f3196 | 953 | |
22e3a02b | 954 | if (pid_is_valid(object_pid) && client_context_get(s, object_pid, NULL, NULL, 0, NULL, &o) >= 0) { |
968f3196 | 955 | |
22e3a02b LP |
956 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->pid, pid_t, pid_is_valid, PID_FMT, "OBJECT_PID"); |
957 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->uid, uid_t, uid_is_valid, UID_FMT, "OBJECT_UID"); | |
958 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->gid, gid_t, gid_is_valid, GID_FMT, "OBJECT_GID"); | |
968f3196 | 959 | |
084eeb86 | 960 | /* See above for size limits, only ->cmdline may be large, so use a heap allocation for it. */ |
22e3a02b LP |
961 | IOVEC_ADD_STRING_FIELD(iovec, n, o->comm, "OBJECT_COMM"); |
962 | IOVEC_ADD_STRING_FIELD(iovec, n, o->exe, "OBJECT_EXE"); | |
084eeb86 ZJS |
963 | if (o->cmdline) |
964 | cmdline2 = set_iovec_string_field(iovec, &n, "OBJECT_CMDLINE=", o->cmdline); | |
968f3196 | 965 | |
084eeb86 | 966 | IOVEC_ADD_STRING_FIELD(iovec, n, o->capeff, "OBJECT_CAP_EFFECTIVE"); |
22e3a02b | 967 | IOVEC_ADD_SIZED_FIELD(iovec, n, o->label, o->label_size, "OBJECT_SELINUX_CONTEXT"); |
22e3a02b LP |
968 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->auditid, uint32_t, audit_session_is_valid, "%" PRIu32, "OBJECT_AUDIT_SESSION"); |
969 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->loginuid, uid_t, uid_is_valid, UID_FMT, "OBJECT_AUDIT_LOGINUID"); | |
d473176a | 970 | |
22e3a02b LP |
971 | IOVEC_ADD_STRING_FIELD(iovec, n, o->cgroup, "OBJECT_SYSTEMD_CGROUP"); |
972 | IOVEC_ADD_STRING_FIELD(iovec, n, o->session, "OBJECT_SYSTEMD_SESSION"); | |
973 | IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->owner_uid, uid_t, uid_is_valid, UID_FMT, "OBJECT_SYSTEMD_OWNER_UID"); | |
974 | IOVEC_ADD_STRING_FIELD(iovec, n, o->unit, "OBJECT_SYSTEMD_UNIT"); | |
975 | IOVEC_ADD_STRING_FIELD(iovec, n, o->user_unit, "OBJECT_SYSTEMD_USER_UNIT"); | |
976 | IOVEC_ADD_STRING_FIELD(iovec, n, o->slice, "OBJECT_SYSTEMD_SLICE"); | |
977 | IOVEC_ADD_STRING_FIELD(iovec, n, o->user_slice, "OBJECT_SYSTEMD_USER_SLICE"); | |
d473176a | 978 | |
22e3a02b | 979 | IOVEC_ADD_ID128_FIELD(iovec, n, o->invocation_id, "OBJECT_SYSTEMD_INVOCATION_ID="); |
968f3196 | 980 | } |
22e3a02b | 981 | |
968f3196 | 982 | assert(n <= m); |
d025f1e4 ZJS |
983 | |
984 | if (tv) { | |
398a50cd | 985 | sprintf(source_time, "_SOURCE_REALTIME_TIMESTAMP=" USEC_FMT, timeval_load(tv)); |
e6a7ec4b | 986 | iovec[n++] = IOVEC_MAKE_STRING(source_time); |
d025f1e4 ZJS |
987 | } |
988 | ||
989 | /* Note that strictly speaking storing the boot id here is | |
990 | * redundant since the entry includes this in-line | |
991 | * anyway. However, we need this indexed, too. */ | |
0c24bb23 | 992 | if (!isempty(s->boot_id_field)) |
e6a7ec4b | 993 | iovec[n++] = IOVEC_MAKE_STRING(s->boot_id_field); |
d025f1e4 | 994 | |
0c24bb23 | 995 | if (!isempty(s->machine_id_field)) |
e6a7ec4b | 996 | iovec[n++] = IOVEC_MAKE_STRING(s->machine_id_field); |
d025f1e4 | 997 | |
0c24bb23 | 998 | if (!isempty(s->hostname_field)) |
e6a7ec4b | 999 | iovec[n++] = IOVEC_MAKE_STRING(s->hostname_field); |
d025f1e4 | 1000 | |
b1852c48 LP |
1001 | if (!isempty(s->namespace_field)) |
1002 | iovec[n++] = IOVEC_MAKE_STRING(s->namespace_field); | |
1003 | ||
d025f1e4 ZJS |
1004 | assert(n <= m); |
1005 | ||
22e3a02b LP |
1006 | if (s->split_mode == SPLIT_UID && c && uid_is_valid(c->uid)) |
1007 | /* Split up strictly by (non-root) UID */ | |
1008 | journal_uid = c->uid; | |
1009 | else if (s->split_mode == SPLIT_LOGIN && c && c->uid > 0 && uid_is_valid(c->owner_uid)) | |
edc3797f LP |
1010 | /* Split up by login UIDs. We do this only if the |
1011 | * realuid is not root, in order not to accidentally | |
1012 | * leak privileged information to the user that is | |
1013 | * logged by a privileged process that is part of an | |
7517e174 | 1014 | * unprivileged session. */ |
22e3a02b | 1015 | journal_uid = c->owner_uid; |
da499392 KS |
1016 | else |
1017 | journal_uid = 0; | |
759c945a | 1018 | |
d07f7b9e | 1019 | write_to_journal(s, journal_uid, iovec, n, priority); |
d025f1e4 ZJS |
1020 | } |
1021 | ||
13181942 | 1022 | void server_driver_message(Server *s, pid_t object_pid, const char *message_id, const char *format, ...) { |
22e3a02b | 1023 | |
d3070fbd LP |
1024 | struct iovec *iovec; |
1025 | size_t n = 0, k, m; | |
d025f1e4 | 1026 | va_list ap; |
22e3a02b | 1027 | int r; |
d025f1e4 ZJS |
1028 | |
1029 | assert(s); | |
1030 | assert(format); | |
1031 | ||
f643ae71 | 1032 | m = N_IOVEC_META_FIELDS + 5 + N_IOVEC_PAYLOAD_FIELDS + client_context_extra_fields_n_iovec(s->my_context) + N_IOVEC_OBJECT_FIELDS; |
d3070fbd LP |
1033 | iovec = newa(struct iovec, m); |
1034 | ||
4850d39a | 1035 | assert_cc(3 == LOG_FAC(LOG_DAEMON)); |
e6a7ec4b LP |
1036 | iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_FACILITY=3"); |
1037 | iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=systemd-journald"); | |
b6fa2555 | 1038 | |
e6a7ec4b | 1039 | iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=driver"); |
4850d39a | 1040 | assert_cc(6 == LOG_INFO); |
e6a7ec4b | 1041 | iovec[n++] = IOVEC_MAKE_STRING("PRIORITY=6"); |
d025f1e4 | 1042 | |
2b044526 | 1043 | if (message_id) |
e6a7ec4b | 1044 | iovec[n++] = IOVEC_MAKE_STRING(message_id); |
d3070fbd | 1045 | k = n; |
8a03c9ef ZJS |
1046 | |
1047 | va_start(ap, format); | |
d3070fbd | 1048 | r = log_format_iovec(iovec, m, &n, false, 0, format, ap); |
32917e33 | 1049 | /* Error handling below */ |
8a03c9ef ZJS |
1050 | va_end(ap); |
1051 | ||
32917e33 | 1052 | if (r >= 0) |
d3070fbd | 1053 | dispatch_message_real(s, iovec, n, m, s->my_context, NULL, LOG_INFO, object_pid); |
8a03c9ef | 1054 | |
d3070fbd LP |
1055 | while (k < n) |
1056 | free(iovec[k++].iov_base); | |
32917e33 ZJS |
1057 | |
1058 | if (r < 0) { | |
1059 | /* We failed to format the message. Emit a warning instead. */ | |
1060 | char buf[LINE_MAX]; | |
1061 | ||
4bbccb02 | 1062 | xsprintf(buf, "MESSAGE=Entry printing failed: %s", strerror_safe(r)); |
32917e33 ZJS |
1063 | |
1064 | n = 3; | |
e6a7ec4b LP |
1065 | iovec[n++] = IOVEC_MAKE_STRING("PRIORITY=4"); |
1066 | iovec[n++] = IOVEC_MAKE_STRING(buf); | |
d3070fbd | 1067 | dispatch_message_real(s, iovec, n, m, s->my_context, NULL, LOG_INFO, object_pid); |
32917e33 | 1068 | } |
d025f1e4 ZJS |
1069 | } |
1070 | ||
1071 | void server_dispatch_message( | |
1072 | Server *s, | |
d3070fbd | 1073 | struct iovec *iovec, size_t n, size_t m, |
22e3a02b | 1074 | ClientContext *c, |
3b3154df | 1075 | const struct timeval *tv, |
968f3196 ZJS |
1076 | int priority, |
1077 | pid_t object_pid) { | |
d025f1e4 | 1078 | |
8580d1f7 | 1079 | uint64_t available = 0; |
22e3a02b | 1080 | int rl; |
d025f1e4 ZJS |
1081 | |
1082 | assert(s); | |
1083 | assert(iovec || n == 0); | |
1084 | ||
1085 | if (n == 0) | |
1086 | return; | |
1087 | ||
1088 | if (LOG_PRI(priority) > s->max_level_store) | |
1089 | return; | |
1090 | ||
2f5df74a HHPF |
1091 | /* Stop early in case the information will not be stored |
1092 | * in a journal. */ | |
1093 | if (s->storage == STORAGE_NONE) | |
1094 | return; | |
1095 | ||
22e3a02b LP |
1096 | if (c && c->unit) { |
1097 | (void) determine_space(s, &available, NULL); | |
d025f1e4 | 1098 | |
5ac1530e | 1099 | rl = journal_ratelimit_test(s->ratelimit, c->unit, c->log_ratelimit_interval, c->log_ratelimit_burst, priority & LOG_PRIMASK, available); |
22e3a02b LP |
1100 | if (rl == 0) |
1101 | return; | |
d025f1e4 | 1102 | |
22e3a02b LP |
1103 | /* Write a suppression message if we suppressed something */ |
1104 | if (rl > 1) | |
13181942 LP |
1105 | server_driver_message(s, c->pid, |
1106 | "MESSAGE_ID=" SD_MESSAGE_JOURNAL_DROPPED_STR, | |
1107 | LOG_MESSAGE("Suppressed %i messages from %s", rl - 1, c->unit), | |
5908ff1c | 1108 | "N_DROPPED=%i", rl - 1, |
22e3a02b | 1109 | NULL); |
d025f1e4 ZJS |
1110 | } |
1111 | ||
22e3a02b | 1112 | dispatch_message_real(s, iovec, n, m, c, tv, priority, object_pid); |
d025f1e4 ZJS |
1113 | } |
1114 | ||
f78273c8 | 1115 | int server_flush_to_var(Server *s, bool require_flag_file) { |
fbb63411 | 1116 | char ts[FORMAT_TIMESPAN_MAX]; |
b1852c48 LP |
1117 | sd_journal *j = NULL; |
1118 | const char *fn; | |
fbb63411 | 1119 | unsigned n = 0; |
b1852c48 | 1120 | usec_t start; |
b4e26d1d | 1121 | int r, k; |
d025f1e4 ZJS |
1122 | |
1123 | assert(s); | |
1124 | ||
f78273c8 | 1125 | if (!IN_SET(s->storage, STORAGE_AUTO, STORAGE_PERSISTENT)) |
d025f1e4 ZJS |
1126 | return 0; |
1127 | ||
b1852c48 LP |
1128 | if (s->namespace) /* Flushing concept does not exist for namespace instances */ |
1129 | return 0; | |
1130 | ||
1131 | if (!s->runtime_journal) /* Nothing to flush? */ | |
d025f1e4 ZJS |
1132 | return 0; |
1133 | ||
b1852c48 | 1134 | if (require_flag_file && !flushed_flag_is_set(s)) |
f78273c8 LP |
1135 | return 0; |
1136 | ||
b4e26d1d | 1137 | (void) system_journal_open(s, true, false); |
d025f1e4 ZJS |
1138 | |
1139 | if (!s->system_journal) | |
1140 | return 0; | |
1141 | ||
b1852c48 | 1142 | log_debug("Flushing to %s...", s->system_storage.path); |
d025f1e4 | 1143 | |
fbb63411 LP |
1144 | start = now(CLOCK_MONOTONIC); |
1145 | ||
d025f1e4 | 1146 | r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY); |
23bbb0de MS |
1147 | if (r < 0) |
1148 | return log_error_errno(r, "Failed to read runtime journal: %m"); | |
d025f1e4 | 1149 | |
93b73b06 LP |
1150 | sd_journal_set_data_threshold(j, 0); |
1151 | ||
d025f1e4 ZJS |
1152 | SD_JOURNAL_FOREACH(j) { |
1153 | Object *o = NULL; | |
1154 | JournalFile *f; | |
1155 | ||
1156 | f = j->current_file; | |
1157 | assert(f && f->current_offset > 0); | |
1158 | ||
fbb63411 LP |
1159 | n++; |
1160 | ||
d025f1e4 ZJS |
1161 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
1162 | if (r < 0) { | |
da927ba9 | 1163 | log_error_errno(r, "Can't read entry: %m"); |
d025f1e4 ZJS |
1164 | goto finish; |
1165 | } | |
1166 | ||
5a271b08 | 1167 | r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset); |
d025f1e4 ZJS |
1168 | if (r >= 0) |
1169 | continue; | |
1170 | ||
1171 | if (!shall_try_append_again(s->system_journal, r)) { | |
da927ba9 | 1172 | log_error_errno(r, "Can't write entry: %m"); |
d025f1e4 ZJS |
1173 | goto finish; |
1174 | } | |
1175 | ||
1176 | server_rotate(s); | |
3a19f215 | 1177 | server_vacuum(s, false); |
d025f1e4 | 1178 | |
253f59df LP |
1179 | if (!s->system_journal) { |
1180 | log_notice("Didn't flush runtime journal since rotation of system journal wasn't successful."); | |
1181 | r = -EIO; | |
1182 | goto finish; | |
1183 | } | |
1184 | ||
d025f1e4 | 1185 | log_debug("Retrying write."); |
5a271b08 | 1186 | r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset); |
d025f1e4 | 1187 | if (r < 0) { |
da927ba9 | 1188 | log_error_errno(r, "Can't write entry: %m"); |
d025f1e4 ZJS |
1189 | goto finish; |
1190 | } | |
1191 | } | |
1192 | ||
804ae586 LP |
1193 | r = 0; |
1194 | ||
d025f1e4 | 1195 | finish: |
fd790d6f RM |
1196 | if (s->system_journal) |
1197 | journal_file_post_change(s->system_journal); | |
d025f1e4 | 1198 | |
804ae586 | 1199 | s->runtime_journal = journal_file_close(s->runtime_journal); |
d025f1e4 ZJS |
1200 | |
1201 | if (r >= 0) | |
b1852c48 | 1202 | (void) rm_rf(s->runtime_storage.path, REMOVE_ROOT); |
d025f1e4 | 1203 | |
763c7aa2 | 1204 | sd_journal_close(j); |
d025f1e4 | 1205 | |
13181942 | 1206 | server_driver_message(s, 0, NULL, |
b1852c48 LP |
1207 | LOG_MESSAGE("Time spent on flushing to %s is %s for %u entries.", |
1208 | s->system_storage.path, | |
8a03c9ef ZJS |
1209 | format_timespan(ts, sizeof(ts), now(CLOCK_MONOTONIC) - start, 0), |
1210 | n), | |
1211 | NULL); | |
fbb63411 | 1212 | |
b1852c48 LP |
1213 | fn = strjoina(s->runtime_directory, "/flushed"); |
1214 | k = touch(fn); | |
b4e26d1d | 1215 | if (k < 0) |
b1852c48 | 1216 | log_warning_errno(k, "Failed to touch %s, ignoring: %m", fn); |
b4e26d1d | 1217 | |
65c398c0 | 1218 | server_refresh_idle_timer(s); |
d025f1e4 ZJS |
1219 | return r; |
1220 | } | |
1221 | ||
b4e26d1d | 1222 | static int server_relinquish_var(Server *s) { |
b1852c48 | 1223 | const char *fn; |
b4e26d1d LP |
1224 | assert(s); |
1225 | ||
1226 | if (s->storage == STORAGE_NONE) | |
1227 | return 0; | |
1228 | ||
b1852c48 LP |
1229 | if (s->namespace) /* Concept does not exist for namespaced instances */ |
1230 | return -EOPNOTSUPP; | |
1231 | ||
b4e26d1d LP |
1232 | if (s->runtime_journal && !s->system_journal) |
1233 | return 0; | |
1234 | ||
b1852c48 | 1235 | log_debug("Relinquishing %s...", s->system_storage.path); |
b4e26d1d LP |
1236 | |
1237 | (void) system_journal_open(s, false, true); | |
1238 | ||
1239 | s->system_journal = journal_file_close(s->system_journal); | |
1240 | ordered_hashmap_clear_with_destructor(s->user_journals, journal_file_close); | |
1241 | set_clear_with_destructor(s->deferred_closes, journal_file_close); | |
1242 | ||
b1852c48 LP |
1243 | fn = strjoina(s->runtime_directory, "/flushed"); |
1244 | if (unlink(fn) < 0 && errno != ENOENT) | |
1245 | log_warning_errno(errno, "Failed to unlink %s, ignoring: %m", fn); | |
b4e26d1d | 1246 | |
65c398c0 | 1247 | server_refresh_idle_timer(s); |
b4e26d1d LP |
1248 | return 0; |
1249 | } | |
1250 | ||
65c398c0 LP |
1251 | int server_process_datagram( |
1252 | sd_event_source *es, | |
1253 | int fd, | |
1254 | uint32_t revents, | |
1255 | void *userdata) { | |
1256 | ||
f9a810be | 1257 | Server *s = userdata; |
a315ac4e LP |
1258 | struct ucred *ucred = NULL; |
1259 | struct timeval *tv = NULL; | |
1260 | struct cmsghdr *cmsg; | |
1261 | char *label = NULL; | |
1262 | size_t label_len = 0, m; | |
1263 | struct iovec iovec; | |
1264 | ssize_t n; | |
1265 | int *fds = NULL, v = 0; | |
da6053d0 | 1266 | size_t n_fds = 0; |
a315ac4e | 1267 | |
fb29cdbe LP |
1268 | /* We use NAME_MAX space for the SELinux label here. The kernel currently enforces no limit, but |
1269 | * according to suggestions from the SELinux people this will change and it will probably be | |
1270 | * identical to NAME_MAX. For now we use that, but this should be updated one day when the final | |
1271 | * limit is known. */ | |
1272 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) + | |
1273 | CMSG_SPACE(sizeof(struct timeval)) + | |
1274 | CMSG_SPACE(sizeof(int)) + /* fd */ | |
1275 | CMSG_SPACE(NAME_MAX) /* selinux label */) control; | |
a315ac4e LP |
1276 | |
1277 | union sockaddr_union sa = {}; | |
1278 | ||
1279 | struct msghdr msghdr = { | |
1280 | .msg_iov = &iovec, | |
1281 | .msg_iovlen = 1, | |
1282 | .msg_control = &control, | |
1283 | .msg_controllen = sizeof(control), | |
1284 | .msg_name = &sa, | |
1285 | .msg_namelen = sizeof(sa), | |
1286 | }; | |
f9a810be | 1287 | |
d025f1e4 | 1288 | assert(s); |
875c2e22 | 1289 | assert(fd == s->native_fd || fd == s->syslog_fd || fd == s->audit_fd); |
f9a810be | 1290 | |
baaa35ad ZJS |
1291 | if (revents != EPOLLIN) |
1292 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
1293 | "Got invalid event from epoll for datagram fd: %" PRIx32, | |
1294 | revents); | |
f9a810be | 1295 | |
22e3a02b LP |
1296 | /* Try to get the right size, if we can. (Not all sockets support SIOCINQ, hence we just try, but don't rely on |
1297 | * it.) */ | |
a315ac4e | 1298 | (void) ioctl(fd, SIOCINQ, &v); |
d025f1e4 | 1299 | |
a315ac4e LP |
1300 | /* Fix it up, if it is too small. We use the same fixed value as auditd here. Awful! */ |
1301 | m = PAGE_ALIGN(MAX3((size_t) v + 1, | |
1302 | (size_t) LINE_MAX, | |
1303 | ALIGN(sizeof(struct nlmsghdr)) + ALIGN((size_t) MAX_AUDIT_MESSAGE_LENGTH)) + 1); | |
d025f1e4 | 1304 | |
a315ac4e LP |
1305 | if (!GREEDY_REALLOC(s->buffer, s->buffer_size, m)) |
1306 | return log_oom(); | |
875c2e22 | 1307 | |
5cfa2c3d | 1308 | iovec = IOVEC_MAKE(s->buffer, s->buffer_size - 1); /* Leave room for trailing NUL we add later */ |
d025f1e4 | 1309 | |
3691bcf3 LP |
1310 | n = recvmsg_safe(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
1311 | if (IN_SET(n, -EINTR, -EAGAIN)) | |
1312 | return 0; | |
1313 | if (n == -EXFULL) { | |
1314 | log_warning("Got message with truncated control data (too many fds sent?), ignoring."); | |
1315 | return 0; | |
a315ac4e | 1316 | } |
3691bcf3 LP |
1317 | if (n < 0) |
1318 | return log_error_errno(n, "recvmsg() failed: %m"); | |
875c2e22 | 1319 | |
bc2762a3 | 1320 | CMSG_FOREACH(cmsg, &msghdr) |
a315ac4e LP |
1321 | if (cmsg->cmsg_level == SOL_SOCKET && |
1322 | cmsg->cmsg_type == SCM_CREDENTIALS && | |
3691bcf3 LP |
1323 | cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { |
1324 | assert(!ucred); | |
a315ac4e | 1325 | ucred = (struct ucred*) CMSG_DATA(cmsg); |
3691bcf3 | 1326 | } else if (cmsg->cmsg_level == SOL_SOCKET && |
a315ac4e | 1327 | cmsg->cmsg_type == SCM_SECURITY) { |
3691bcf3 | 1328 | assert(!label); |
a315ac4e LP |
1329 | label = (char*) CMSG_DATA(cmsg); |
1330 | label_len = cmsg->cmsg_len - CMSG_LEN(0); | |
1331 | } else if (cmsg->cmsg_level == SOL_SOCKET && | |
1332 | cmsg->cmsg_type == SO_TIMESTAMP && | |
3691bcf3 LP |
1333 | cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval))) { |
1334 | assert(!tv); | |
a315ac4e | 1335 | tv = (struct timeval*) CMSG_DATA(cmsg); |
3691bcf3 | 1336 | } else if (cmsg->cmsg_level == SOL_SOCKET && |
a315ac4e | 1337 | cmsg->cmsg_type == SCM_RIGHTS) { |
3691bcf3 | 1338 | assert(!fds); |
a315ac4e LP |
1339 | fds = (int*) CMSG_DATA(cmsg); |
1340 | n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); | |
d025f1e4 ZJS |
1341 | } |
1342 | ||
a315ac4e LP |
1343 | /* And a trailing NUL, just in case */ |
1344 | s->buffer[n] = 0; | |
1345 | ||
1346 | if (fd == s->syslog_fd) { | |
1347 | if (n > 0 && n_fds == 0) | |
bb3ff70a | 1348 | server_process_syslog_message(s, s->buffer, n, ucred, tv, label, label_len); |
a315ac4e LP |
1349 | else if (n_fds > 0) |
1350 | log_warning("Got file descriptors via syslog socket. Ignoring."); | |
1351 | ||
1352 | } else if (fd == s->native_fd) { | |
1353 | if (n > 0 && n_fds == 0) | |
1354 | server_process_native_message(s, s->buffer, n, ucred, tv, label, label_len); | |
1355 | else if (n == 0 && n_fds == 1) | |
1356 | server_process_native_file(s, fds[0], ucred, tv, label, label_len); | |
1357 | else if (n_fds > 0) | |
1358 | log_warning("Got too many file descriptors via native socket. Ignoring."); | |
1359 | ||
1360 | } else { | |
1361 | assert(fd == s->audit_fd); | |
1362 | ||
1363 | if (n > 0 && n_fds == 0) | |
1364 | server_process_audit_message(s, s->buffer, n, ucred, &sa, msghdr.msg_namelen); | |
1365 | else if (n_fds > 0) | |
1366 | log_warning("Got file descriptors via audit socket. Ignoring."); | |
f9a810be | 1367 | } |
a315ac4e LP |
1368 | |
1369 | close_many(fds, n_fds); | |
65c398c0 LP |
1370 | |
1371 | server_refresh_idle_timer(s); | |
a315ac4e | 1372 | return 0; |
f9a810be | 1373 | } |
d025f1e4 | 1374 | |
1ec23479 | 1375 | static void server_full_flush(Server *s) { |
f9a810be | 1376 | assert(s); |
d025f1e4 | 1377 | |
f78273c8 | 1378 | (void) server_flush_to_var(s, false); |
f9a810be | 1379 | server_sync(s); |
3a19f215 | 1380 | server_vacuum(s, false); |
d025f1e4 | 1381 | |
18e758bf | 1382 | server_space_usage_message(s, NULL); |
65c398c0 LP |
1383 | |
1384 | server_refresh_idle_timer(s); | |
f9a810be | 1385 | } |
d025f1e4 | 1386 | |
1ec23479 | 1387 | static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) { |
f9a810be | 1388 | Server *s = userdata; |
1ec23479 LP |
1389 | |
1390 | assert(s); | |
1391 | ||
b1852c48 LP |
1392 | if (s->namespace) { |
1393 | log_error("Received SIGUSR1 signal from PID " PID_FMT ", but flushing runtime journals not supported for namespaced instances.", si->ssi_pid); | |
1394 | return 0; | |
1395 | } | |
1396 | ||
b4e26d1d | 1397 | log_info("Received SIGUSR1 signal from PID " PID_FMT ", as request to flush runtime journal.", si->ssi_pid); |
1ec23479 LP |
1398 | server_full_flush(s); |
1399 | ||
1400 | return 0; | |
1401 | } | |
1402 | ||
1403 | static void server_full_rotate(Server *s) { | |
b1852c48 | 1404 | const char *fn; |
33d52ab9 | 1405 | int r; |
d025f1e4 | 1406 | |
f9a810be | 1407 | assert(s); |
d025f1e4 | 1408 | |
f9a810be | 1409 | server_rotate(s); |
3a19f215 FB |
1410 | server_vacuum(s, true); |
1411 | ||
1412 | if (s->system_journal) | |
1413 | patch_min_use(&s->system_storage); | |
1414 | if (s->runtime_journal) | |
1415 | patch_min_use(&s->runtime_storage); | |
d025f1e4 | 1416 | |
dbd6e31c | 1417 | /* Let clients know when the most recent rotation happened. */ |
b1852c48 LP |
1418 | fn = strjoina(s->runtime_directory, "/rotated"); |
1419 | r = write_timestamp_file_atomic(fn, now(CLOCK_MONOTONIC)); | |
33d52ab9 | 1420 | if (r < 0) |
b1852c48 | 1421 | log_warning_errno(r, "Failed to write %s, ignoring: %m", fn); |
1ec23479 LP |
1422 | } |
1423 | ||
1424 | static int dispatch_sigusr2(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) { | |
1425 | Server *s = userdata; | |
1426 | ||
1427 | assert(s); | |
1428 | ||
b4e26d1d | 1429 | log_info("Received SIGUSR2 signal from PID " PID_FMT ", as request to rotate journal.", si->ssi_pid); |
1ec23479 | 1430 | server_full_rotate(s); |
dbd6e31c | 1431 | |
f9a810be LP |
1432 | return 0; |
1433 | } | |
d025f1e4 | 1434 | |
f9a810be LP |
1435 | static int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) { |
1436 | Server *s = userdata; | |
d025f1e4 | 1437 | |
f9a810be | 1438 | assert(s); |
d025f1e4 | 1439 | |
4daf54a8 | 1440 | log_received_signal(LOG_INFO, si); |
d025f1e4 | 1441 | |
6203e07a | 1442 | sd_event_exit(s->event, 0); |
d025f1e4 ZJS |
1443 | return 0; |
1444 | } | |
1445 | ||
1ec23479 | 1446 | static void server_full_sync(Server *s) { |
b1852c48 | 1447 | const char *fn; |
33d52ab9 | 1448 | int r; |
94b65516 LP |
1449 | |
1450 | assert(s); | |
1451 | ||
94b65516 LP |
1452 | server_sync(s); |
1453 | ||
1454 | /* Let clients know when the most recent sync happened. */ | |
b1852c48 LP |
1455 | fn = strjoina(s->runtime_directory, "/synced"); |
1456 | r = write_timestamp_file_atomic(fn, now(CLOCK_MONOTONIC)); | |
33d52ab9 | 1457 | if (r < 0) |
b1852c48 | 1458 | log_warning_errno(r, "Failed to write %s, ignoring: %m", fn); |
94b65516 | 1459 | |
1ec23479 LP |
1460 | return; |
1461 | } | |
1462 | ||
1463 | static int dispatch_sigrtmin1(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) { | |
1464 | Server *s = userdata; | |
1465 | ||
1466 | assert(s); | |
1467 | ||
b4e26d1d | 1468 | log_debug("Received SIGRTMIN1 signal from PID " PID_FMT ", as request to sync.", si->ssi_pid ); |
1ec23479 LP |
1469 | server_full_sync(s); |
1470 | ||
94b65516 LP |
1471 | return 0; |
1472 | } | |
1473 | ||
f9a810be | 1474 | static int setup_signals(Server *s) { |
f9a810be | 1475 | int r; |
d025f1e4 ZJS |
1476 | |
1477 | assert(s); | |
1478 | ||
9bab3b65 | 1479 | assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0); |
d025f1e4 | 1480 | |
151b9b96 | 1481 | r = sd_event_add_signal(s->event, &s->sigusr1_event_source, SIGUSR1, dispatch_sigusr1, s); |
f9a810be LP |
1482 | if (r < 0) |
1483 | return r; | |
1484 | ||
151b9b96 | 1485 | r = sd_event_add_signal(s->event, &s->sigusr2_event_source, SIGUSR2, dispatch_sigusr2, s); |
f9a810be LP |
1486 | if (r < 0) |
1487 | return r; | |
d025f1e4 | 1488 | |
151b9b96 | 1489 | r = sd_event_add_signal(s->event, &s->sigterm_event_source, SIGTERM, dispatch_sigterm, s); |
f9a810be LP |
1490 | if (r < 0) |
1491 | return r; | |
d025f1e4 | 1492 | |
337fabf7 | 1493 | /* Let's process SIGTERM late, so that we flush all queued messages to disk before we exit */ |
b374689c LP |
1494 | r = sd_event_source_set_priority(s->sigterm_event_source, SD_EVENT_PRIORITY_NORMAL+20); |
1495 | if (r < 0) | |
1496 | return r; | |
1497 | ||
337fabf7 LP |
1498 | /* When journald is invoked on the terminal (when debugging), it's useful if C-c is handled |
1499 | * equivalent to SIGTERM. */ | |
151b9b96 | 1500 | r = sd_event_add_signal(s->event, &s->sigint_event_source, SIGINT, dispatch_sigterm, s); |
f9a810be LP |
1501 | if (r < 0) |
1502 | return r; | |
d025f1e4 | 1503 | |
b374689c LP |
1504 | r = sd_event_source_set_priority(s->sigint_event_source, SD_EVENT_PRIORITY_NORMAL+20); |
1505 | if (r < 0) | |
1506 | return r; | |
1507 | ||
337fabf7 LP |
1508 | /* SIGRTMIN+1 causes an immediate sync. We process this very late, so that everything else queued at |
1509 | * this point is really written to disk. Clients can watch /run/systemd/journal/synced with inotify | |
1510 | * until its mtime changes to see when a sync happened. */ | |
94b65516 LP |
1511 | r = sd_event_add_signal(s->event, &s->sigrtmin1_event_source, SIGRTMIN+1, dispatch_sigrtmin1, s); |
1512 | if (r < 0) | |
1513 | return r; | |
1514 | ||
1515 | r = sd_event_source_set_priority(s->sigrtmin1_event_source, SD_EVENT_PRIORITY_NORMAL+15); | |
1516 | if (r < 0) | |
1517 | return r; | |
1518 | ||
d025f1e4 ZJS |
1519 | return 0; |
1520 | } | |
1521 | ||
5707ecf3 ZJS |
1522 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
1523 | Server *s = data; | |
74df0fca | 1524 | int r; |
d025f1e4 | 1525 | |
5707ecf3 | 1526 | assert(s); |
d025f1e4 | 1527 | |
1d84ad94 LP |
1528 | if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_syslog")) { |
1529 | ||
5707ecf3 | 1530 | r = value ? parse_boolean(value) : true; |
d581d9d9 | 1531 | if (r < 0) |
5707ecf3 ZJS |
1532 | log_warning("Failed to parse forward to syslog switch \"%s\". Ignoring.", value); |
1533 | else | |
1534 | s->forward_to_syslog = r; | |
1d84ad94 LP |
1535 | |
1536 | } else if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_kmsg")) { | |
1537 | ||
5707ecf3 ZJS |
1538 | r = value ? parse_boolean(value) : true; |
1539 | if (r < 0) | |
1540 | log_warning("Failed to parse forward to kmsg switch \"%s\". Ignoring.", value); | |
1541 | else | |
1542 | s->forward_to_kmsg = r; | |
1d84ad94 LP |
1543 | |
1544 | } else if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_console")) { | |
1545 | ||
5707ecf3 ZJS |
1546 | r = value ? parse_boolean(value) : true; |
1547 | if (r < 0) | |
1548 | log_warning("Failed to parse forward to console switch \"%s\". Ignoring.", value); | |
1549 | else | |
1550 | s->forward_to_console = r; | |
1d84ad94 LP |
1551 | |
1552 | } else if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_wall")) { | |
1553 | ||
5707ecf3 ZJS |
1554 | r = value ? parse_boolean(value) : true; |
1555 | if (r < 0) | |
1556 | log_warning("Failed to parse forward to wall switch \"%s\". Ignoring.", value); | |
1557 | else | |
1558 | s->forward_to_wall = r; | |
1d84ad94 LP |
1559 | |
1560 | } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_console")) { | |
1561 | ||
1562 | if (proc_cmdline_value_missing(key, value)) | |
1563 | return 0; | |
1564 | ||
5707ecf3 ZJS |
1565 | r = log_level_from_string(value); |
1566 | if (r < 0) | |
1567 | log_warning("Failed to parse max level console value \"%s\". Ignoring.", value); | |
1568 | else | |
1569 | s->max_level_console = r; | |
1d84ad94 LP |
1570 | |
1571 | } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_store")) { | |
1572 | ||
1573 | if (proc_cmdline_value_missing(key, value)) | |
1574 | return 0; | |
1575 | ||
5707ecf3 ZJS |
1576 | r = log_level_from_string(value); |
1577 | if (r < 0) | |
1578 | log_warning("Failed to parse max level store value \"%s\". Ignoring.", value); | |
1579 | else | |
1580 | s->max_level_store = r; | |
1d84ad94 LP |
1581 | |
1582 | } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_syslog")) { | |
1583 | ||
1584 | if (proc_cmdline_value_missing(key, value)) | |
1585 | return 0; | |
1586 | ||
5707ecf3 ZJS |
1587 | r = log_level_from_string(value); |
1588 | if (r < 0) | |
1589 | log_warning("Failed to parse max level syslog value \"%s\". Ignoring.", value); | |
1590 | else | |
1591 | s->max_level_syslog = r; | |
1d84ad94 LP |
1592 | |
1593 | } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_kmsg")) { | |
1594 | ||
1595 | if (proc_cmdline_value_missing(key, value)) | |
1596 | return 0; | |
1597 | ||
5707ecf3 ZJS |
1598 | r = log_level_from_string(value); |
1599 | if (r < 0) | |
1600 | log_warning("Failed to parse max level kmsg value \"%s\". Ignoring.", value); | |
1601 | else | |
1602 | s->max_level_kmsg = r; | |
1d84ad94 LP |
1603 | |
1604 | } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_wall")) { | |
1605 | ||
1606 | if (proc_cmdline_value_missing(key, value)) | |
1607 | return 0; | |
1608 | ||
5707ecf3 ZJS |
1609 | r = log_level_from_string(value); |
1610 | if (r < 0) | |
1611 | log_warning("Failed to parse max level wall value \"%s\". Ignoring.", value); | |
1612 | else | |
1613 | s->max_level_wall = r; | |
1d84ad94 | 1614 | |
5707ecf3 ZJS |
1615 | } else if (startswith(key, "systemd.journald")) |
1616 | log_warning("Unknown journald kernel command line option \"%s\". Ignoring.", key); | |
d025f1e4 | 1617 | |
804ae586 | 1618 | /* do not warn about state here, since probably systemd already did */ |
db91ea32 | 1619 | return 0; |
d025f1e4 ZJS |
1620 | } |
1621 | ||
1622 | static int server_parse_config_file(Server *s) { | |
b1852c48 LP |
1623 | int r; |
1624 | ||
d025f1e4 ZJS |
1625 | assert(s); |
1626 | ||
b1852c48 LP |
1627 | if (s->namespace) { |
1628 | const char *namespaced; | |
1629 | ||
1630 | /* If we are running in namespace mode, load the namespace specific configuration file, and nothing else */ | |
1631 | namespaced = strjoina(PKGSYSCONFDIR "/journald@", s->namespace, ".conf"); | |
1632 | ||
4f9ff96a LP |
1633 | r = config_parse(NULL, |
1634 | namespaced, NULL, | |
1635 | "Journal\0", | |
1636 | config_item_perf_lookup, journald_gperf_lookup, | |
1637 | CONFIG_PARSE_WARN, s, | |
1638 | NULL); | |
b1852c48 LP |
1639 | if (r < 0) |
1640 | return r; | |
1641 | ||
1642 | return 0; | |
1643 | } | |
1644 | ||
4f9ff96a LP |
1645 | return config_parse_many_nulstr( |
1646 | PKGSYSCONFDIR "/journald.conf", | |
1647 | CONF_PATHS_NULSTR("systemd/journald.conf.d"), | |
1648 | "Journal\0", | |
1649 | config_item_perf_lookup, journald_gperf_lookup, | |
1650 | CONFIG_PARSE_WARN, s, NULL); | |
d025f1e4 ZJS |
1651 | } |
1652 | ||
f9a810be LP |
1653 | static int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) { |
1654 | Server *s = userdata; | |
26687bf8 OS |
1655 | |
1656 | assert(s); | |
1657 | ||
f9a810be | 1658 | server_sync(s); |
26687bf8 OS |
1659 | return 0; |
1660 | } | |
1661 | ||
d07f7b9e | 1662 | int server_schedule_sync(Server *s, int priority) { |
26687bf8 OS |
1663 | int r; |
1664 | ||
26687bf8 OS |
1665 | assert(s); |
1666 | ||
d07f7b9e LP |
1667 | if (priority <= LOG_CRIT) { |
1668 | /* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */ | |
1669 | server_sync(s); | |
1670 | return 0; | |
1671 | } | |
1672 | ||
26687bf8 OS |
1673 | if (s->sync_scheduled) |
1674 | return 0; | |
1675 | ||
f9a810be | 1676 | if (s->sync_interval_usec > 0) { |
f9a810be LP |
1677 | |
1678 | if (!s->sync_event_source) { | |
39cf0351 | 1679 | r = sd_event_add_time_relative( |
6a0f1f6d LP |
1680 | s->event, |
1681 | &s->sync_event_source, | |
1682 | CLOCK_MONOTONIC, | |
39cf0351 | 1683 | s->sync_interval_usec, 0, |
6a0f1f6d | 1684 | server_dispatch_sync, s); |
f9a810be LP |
1685 | if (r < 0) |
1686 | return r; | |
1687 | ||
1688 | r = sd_event_source_set_priority(s->sync_event_source, SD_EVENT_PRIORITY_IMPORTANT); | |
1689 | } else { | |
39cf0351 | 1690 | r = sd_event_source_set_time_relative(s->sync_event_source, s->sync_interval_usec); |
f9a810be LP |
1691 | if (r < 0) |
1692 | return r; | |
1693 | ||
1694 | r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_ONESHOT); | |
1695 | } | |
26687bf8 | 1696 | if (r < 0) |
f9a810be | 1697 | return r; |
26687bf8 | 1698 | |
f9a810be LP |
1699 | s->sync_scheduled = true; |
1700 | } | |
26687bf8 OS |
1701 | |
1702 | return 0; | |
1703 | } | |
1704 | ||
0c24bb23 LP |
1705 | static int dispatch_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
1706 | Server *s = userdata; | |
1707 | ||
1708 | assert(s); | |
1709 | ||
1710 | server_cache_hostname(s); | |
1711 | return 0; | |
1712 | } | |
1713 | ||
1714 | static int server_open_hostname(Server *s) { | |
1715 | int r; | |
1716 | ||
1717 | assert(s); | |
1718 | ||
db4a47e9 LP |
1719 | s->hostname_fd = open("/proc/sys/kernel/hostname", |
1720 | O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
4a62c710 MS |
1721 | if (s->hostname_fd < 0) |
1722 | return log_error_errno(errno, "Failed to open /proc/sys/kernel/hostname: %m"); | |
0c24bb23 | 1723 | |
151b9b96 | 1724 | r = sd_event_add_io(s->event, &s->hostname_event_source, s->hostname_fd, 0, dispatch_hostname_change, s); |
0c24bb23 | 1725 | if (r < 0) { |
28def94c DR |
1726 | /* kernels prior to 3.2 don't support polling this file. Ignore |
1727 | * the failure. */ | |
1728 | if (r == -EPERM) { | |
e53fc357 | 1729 | log_warning_errno(r, "Failed to register hostname fd in event loop, ignoring: %m"); |
03e334a1 | 1730 | s->hostname_fd = safe_close(s->hostname_fd); |
28def94c DR |
1731 | return 0; |
1732 | } | |
1733 | ||
23bbb0de | 1734 | return log_error_errno(r, "Failed to register hostname fd in event loop: %m"); |
0c24bb23 LP |
1735 | } |
1736 | ||
1737 | r = sd_event_source_set_priority(s->hostname_event_source, SD_EVENT_PRIORITY_IMPORTANT-10); | |
23bbb0de | 1738 | if (r < 0) |
38b38500 | 1739 | return log_error_errno(r, "Failed to adjust priority of hostname event source: %m"); |
0c24bb23 LP |
1740 | |
1741 | return 0; | |
1742 | } | |
1743 | ||
e22aa3d3 LP |
1744 | static int dispatch_notify_event(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
1745 | Server *s = userdata; | |
1746 | int r; | |
1747 | ||
1748 | assert(s); | |
1749 | assert(s->notify_event_source == es); | |
1750 | assert(s->notify_fd == fd); | |
1751 | ||
e22aa3d3 | 1752 | /* The $NOTIFY_SOCKET is writable again, now send exactly one |
dd835265 | 1753 | * message on it. Either it's the watchdog event, the initial |
119e9655 LP |
1754 | * READY=1 event or an stdout stream event. If there's nothing |
1755 | * to write anymore, turn our event source off. The next time | |
1756 | * there's something to send it will be turned on again. */ | |
e22aa3d3 LP |
1757 | |
1758 | if (!s->sent_notify_ready) { | |
1759 | static const char p[] = | |
1760 | "READY=1\n" | |
1761 | "STATUS=Processing requests..."; | |
1762 | ssize_t l; | |
1763 | ||
1764 | l = send(s->notify_fd, p, strlen(p), MSG_DONTWAIT); | |
1765 | if (l < 0) { | |
1766 | if (errno == EAGAIN) | |
1767 | return 0; | |
1768 | ||
1769 | return log_error_errno(errno, "Failed to send READY=1 notification message: %m"); | |
1770 | } | |
1771 | ||
1772 | s->sent_notify_ready = true; | |
1773 | log_debug("Sent READY=1 notification."); | |
1774 | ||
119e9655 LP |
1775 | } else if (s->send_watchdog) { |
1776 | ||
1777 | static const char p[] = | |
1778 | "WATCHDOG=1"; | |
1779 | ||
1780 | ssize_t l; | |
1781 | ||
1782 | l = send(s->notify_fd, p, strlen(p), MSG_DONTWAIT); | |
1783 | if (l < 0) { | |
1784 | if (errno == EAGAIN) | |
1785 | return 0; | |
1786 | ||
1787 | return log_error_errno(errno, "Failed to send WATCHDOG=1 notification message: %m"); | |
1788 | } | |
1789 | ||
1790 | s->send_watchdog = false; | |
1791 | log_debug("Sent WATCHDOG=1 notification."); | |
1792 | ||
e22aa3d3 LP |
1793 | } else if (s->stdout_streams_notify_queue) |
1794 | /* Dispatch one stream notification event */ | |
1795 | stdout_stream_send_notify(s->stdout_streams_notify_queue); | |
1796 | ||
61233823 | 1797 | /* Leave us enabled if there's still more to do. */ |
119e9655 | 1798 | if (s->send_watchdog || s->stdout_streams_notify_queue) |
e22aa3d3 LP |
1799 | return 0; |
1800 | ||
1801 | /* There was nothing to do anymore, let's turn ourselves off. */ | |
1802 | r = sd_event_source_set_enabled(es, SD_EVENT_OFF); | |
1803 | if (r < 0) | |
1804 | return log_error_errno(r, "Failed to turn off notify event source: %m"); | |
1805 | ||
1806 | return 0; | |
1807 | } | |
1808 | ||
119e9655 LP |
1809 | static int dispatch_watchdog(sd_event_source *es, uint64_t usec, void *userdata) { |
1810 | Server *s = userdata; | |
1811 | int r; | |
1812 | ||
1813 | assert(s); | |
1814 | ||
1815 | s->send_watchdog = true; | |
1816 | ||
1817 | r = sd_event_source_set_enabled(s->notify_event_source, SD_EVENT_ON); | |
1818 | if (r < 0) | |
1819 | log_warning_errno(r, "Failed to turn on notify event source: %m"); | |
1820 | ||
1821 | r = sd_event_source_set_time(s->watchdog_event_source, usec + s->watchdog_usec / 2); | |
1822 | if (r < 0) | |
1823 | return log_error_errno(r, "Failed to restart watchdog event source: %m"); | |
1824 | ||
1825 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ON); | |
1826 | if (r < 0) | |
1827 | return log_error_errno(r, "Failed to enable watchdog event source: %m"); | |
1828 | ||
1829 | return 0; | |
1830 | } | |
1831 | ||
e22aa3d3 | 1832 | static int server_connect_notify(Server *s) { |
f36a9d59 ZJS |
1833 | union sockaddr_union sa; |
1834 | socklen_t sa_len; | |
e22aa3d3 | 1835 | const char *e; |
f36a9d59 | 1836 | int r; |
e22aa3d3 LP |
1837 | |
1838 | assert(s); | |
1839 | assert(s->notify_fd < 0); | |
1840 | assert(!s->notify_event_source); | |
1841 | ||
1842 | /* | |
337fabf7 LP |
1843 | * So here's the problem: we'd like to send notification messages to PID 1, but we cannot do that via |
1844 | * sd_notify(), since that's synchronous, and we might end up blocking on it. Specifically: given | |
1845 | * that PID 1 might block on dbus-daemon during IPC, and dbus-daemon is logging to us, and might | |
1846 | * hence block on us, we might end up in a deadlock if we block on sending PID 1 notification | |
1847 | * messages — by generating a full blocking circle. To avoid this, let's create a non-blocking | |
1848 | * socket, and connect it to the notification socket, and then wait for POLLOUT before we send | |
1849 | * anything. This should efficiently avoid any deadlocks, as we'll never block on PID 1, hence PID 1 | |
1850 | * can safely block on dbus-daemon which can safely block on us again. | |
1851 | * | |
1852 | * Don't think that this issue is real? It is, see: https://github.com/systemd/systemd/issues/1505 | |
1853 | */ | |
e22aa3d3 LP |
1854 | |
1855 | e = getenv("NOTIFY_SOCKET"); | |
1856 | if (!e) | |
1857 | return 0; | |
1858 | ||
f36a9d59 ZJS |
1859 | r = sockaddr_un_set_path(&sa.un, e); |
1860 | if (r < 0) | |
1861 | return log_error_errno(r, "NOTIFY_SOCKET set to invalid value '%s': %m", e); | |
1862 | sa_len = r; | |
e22aa3d3 LP |
1863 | |
1864 | s->notify_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
1865 | if (s->notify_fd < 0) | |
1866 | return log_error_errno(errno, "Failed to create notify socket: %m"); | |
1867 | ||
1868 | (void) fd_inc_sndbuf(s->notify_fd, NOTIFY_SNDBUF_SIZE); | |
1869 | ||
f36a9d59 | 1870 | r = connect(s->notify_fd, &sa.sa, sa_len); |
e22aa3d3 LP |
1871 | if (r < 0) |
1872 | return log_error_errno(errno, "Failed to connect to notify socket: %m"); | |
1873 | ||
1874 | r = sd_event_add_io(s->event, &s->notify_event_source, s->notify_fd, EPOLLOUT, dispatch_notify_event, s); | |
1875 | if (r < 0) | |
1876 | return log_error_errno(r, "Failed to watch notification socket: %m"); | |
1877 | ||
119e9655 LP |
1878 | if (sd_watchdog_enabled(false, &s->watchdog_usec) > 0) { |
1879 | s->send_watchdog = true; | |
1880 | ||
39cf0351 | 1881 | 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); |
119e9655 LP |
1882 | if (r < 0) |
1883 | return log_error_errno(r, "Failed to add watchdog time event: %m"); | |
1884 | } | |
1885 | ||
337fabf7 | 1886 | /* This should fire pretty soon, which we'll use to send the READY=1 event. */ |
e22aa3d3 LP |
1887 | |
1888 | return 0; | |
1889 | } | |
1890 | ||
4f413af2 LP |
1891 | static int synchronize_second_half(sd_event_source *event_source, void *userdata) { |
1892 | Varlink *link = userdata; | |
1893 | Server *s; | |
1894 | int r; | |
1895 | ||
1896 | assert(link); | |
1897 | assert_se(s = varlink_get_userdata(link)); | |
1898 | ||
1899 | /* This is the "second half" of the Synchronize() varlink method. This function is called as deferred | |
1900 | * event source at a low priority to ensure the synchronization completes after all queued log | |
1901 | * messages are processed. */ | |
1902 | server_full_sync(s); | |
1903 | ||
1904 | /* Let's get rid of the event source now, by marking it as non-floating again. It then has no ref | |
1905 | * anymore and is immediately destroyed after we return from this function, i.e. from this event | |
1906 | * source handler at the end. */ | |
1907 | r = sd_event_source_set_floating(event_source, false); | |
1908 | if (r < 0) | |
1909 | return log_error_errno(r, "Failed to mark event source as non-floating: %m"); | |
1910 | ||
1911 | return varlink_reply(link, NULL); | |
1912 | } | |
1913 | ||
1914 | static void synchronize_destroy(void *userdata) { | |
1915 | varlink_unref(userdata); | |
1916 | } | |
1917 | ||
1ec23479 | 1918 | static int vl_method_synchronize(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { |
4f413af2 | 1919 | _cleanup_(sd_event_source_unrefp) sd_event_source *event_source = NULL; |
1ec23479 | 1920 | Server *s = userdata; |
4f413af2 | 1921 | int r; |
1ec23479 LP |
1922 | |
1923 | assert(link); | |
1924 | assert(s); | |
1925 | ||
1926 | if (json_variant_elements(parameters) > 0) | |
1927 | return varlink_error_invalid_parameter(link, parameters); | |
1928 | ||
1929 | log_info("Received client request to rotate journal."); | |
1ec23479 | 1930 | |
4f413af2 LP |
1931 | /* We don't do the main work now, but instead enqueue a deferred event loop job which will do |
1932 | * it. That job is scheduled at low priority, so that we return from this method call only after all | |
1933 | * queued but not processed log messages are written to disk, so that this method call returning can | |
1934 | * be used as nice synchronization point. */ | |
1935 | r = sd_event_add_defer(s->event, &event_source, synchronize_second_half, link); | |
1936 | if (r < 0) | |
1937 | return log_error_errno(r, "Failed to allocate defer event source: %m"); | |
1938 | ||
1939 | r = sd_event_source_set_destroy_callback(event_source, synchronize_destroy); | |
1940 | if (r < 0) | |
1941 | return log_error_errno(r, "Failed to set event source destroy callback: %m"); | |
1942 | ||
162392b7 | 1943 | varlink_ref(link); /* The varlink object is now left to the destroy callback to unref */ |
4f413af2 LP |
1944 | |
1945 | r = sd_event_source_set_priority(event_source, SD_EVENT_PRIORITY_NORMAL+15); | |
1946 | if (r < 0) | |
1947 | return log_error_errno(r, "Failed to set defer event source priority: %m"); | |
1948 | ||
1949 | /* Give up ownership of this event source. It will now be destroyed along with event loop itself, | |
1950 | * unless it destroys itself earlier. */ | |
1951 | r = sd_event_source_set_floating(event_source, true); | |
1952 | if (r < 0) | |
1953 | return log_error_errno(r, "Failed to mark event source as floating: %m"); | |
1954 | ||
1955 | (void) sd_event_source_set_description(event_source, "deferred-sync"); | |
1956 | ||
1957 | return 0; | |
1ec23479 LP |
1958 | } |
1959 | ||
1960 | static int vl_method_rotate(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { | |
1961 | Server *s = userdata; | |
1962 | ||
1963 | assert(link); | |
1964 | assert(s); | |
1965 | ||
1966 | if (json_variant_elements(parameters) > 0) | |
1967 | return varlink_error_invalid_parameter(link, parameters); | |
1968 | ||
1969 | log_info("Received client request to rotate journal."); | |
1970 | server_full_rotate(s); | |
1971 | ||
1972 | return varlink_reply(link, NULL); | |
1973 | } | |
1974 | ||
1975 | static int vl_method_flush_to_var(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { | |
1976 | Server *s = userdata; | |
1977 | ||
1978 | assert(link); | |
1979 | assert(s); | |
1980 | ||
1981 | if (json_variant_elements(parameters) > 0) | |
1982 | return varlink_error_invalid_parameter(link, parameters); | |
b1852c48 LP |
1983 | if (s->namespace) |
1984 | return varlink_error(link, "io.systemd.Journal.NotSupportedByNamespaces", NULL); | |
1ec23479 LP |
1985 | |
1986 | log_info("Received client request to flush runtime journal."); | |
1987 | server_full_flush(s); | |
1988 | ||
1989 | return varlink_reply(link, NULL); | |
1990 | } | |
1991 | ||
b4e26d1d LP |
1992 | static int vl_method_relinquish_var(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { |
1993 | Server *s = userdata; | |
1994 | ||
1995 | assert(link); | |
1996 | assert(s); | |
1997 | ||
1998 | if (json_variant_elements(parameters) > 0) | |
1999 | return varlink_error_invalid_parameter(link, parameters); | |
b1852c48 LP |
2000 | if (s->namespace) |
2001 | return varlink_error(link, "io.systemd.Journal.NotSupportedByNamespaces", NULL); | |
b4e26d1d | 2002 | |
b1852c48 | 2003 | log_info("Received client request to relinquish %s access.", s->system_storage.path); |
b4e26d1d LP |
2004 | server_relinquish_var(s); |
2005 | ||
2006 | return varlink_reply(link, NULL); | |
2007 | } | |
2008 | ||
65c398c0 LP |
2009 | static int vl_connect(VarlinkServer *server, Varlink *link, void *userdata) { |
2010 | Server *s = userdata; | |
2011 | ||
2012 | assert(server); | |
2013 | assert(link); | |
2014 | assert(s); | |
2015 | ||
2016 | (void) server_start_or_stop_idle_timer(s); /* maybe we are no longer idle */ | |
2017 | ||
2018 | return 0; | |
2019 | } | |
2020 | ||
2021 | static void vl_disconnect(VarlinkServer *server, Varlink *link, void *userdata) { | |
2022 | Server *s = userdata; | |
2023 | ||
2024 | assert(server); | |
2025 | assert(link); | |
2026 | assert(s); | |
2027 | ||
2028 | (void) server_start_or_stop_idle_timer(s); /* maybe we are idle now */ | |
2029 | } | |
2030 | ||
dc5437c7 | 2031 | static int server_open_varlink(Server *s, const char *socket, int fd) { |
1ec23479 LP |
2032 | int r; |
2033 | ||
2034 | assert(s); | |
2035 | ||
9807fdc1 | 2036 | r = varlink_server_new(&s->varlink_server, VARLINK_SERVER_ROOT_ONLY|VARLINK_SERVER_INHERIT_USERDATA); |
1ec23479 LP |
2037 | if (r < 0) |
2038 | return r; | |
2039 | ||
2040 | varlink_server_set_userdata(s->varlink_server, s); | |
2041 | ||
2042 | r = varlink_server_bind_method_many( | |
2043 | s->varlink_server, | |
b4e26d1d LP |
2044 | "io.systemd.Journal.Synchronize", vl_method_synchronize, |
2045 | "io.systemd.Journal.Rotate", vl_method_rotate, | |
2046 | "io.systemd.Journal.FlushToVar", vl_method_flush_to_var, | |
2047 | "io.systemd.Journal.RelinquishVar", vl_method_relinquish_var); | |
1ec23479 LP |
2048 | if (r < 0) |
2049 | return r; | |
2050 | ||
65c398c0 LP |
2051 | r = varlink_server_bind_connect(s->varlink_server, vl_connect); |
2052 | if (r < 0) | |
2053 | return r; | |
2054 | ||
2055 | r = varlink_server_bind_disconnect(s->varlink_server, vl_disconnect); | |
2056 | if (r < 0) | |
2057 | return r; | |
2058 | ||
dc5437c7 LP |
2059 | if (fd < 0) |
2060 | r = varlink_server_listen_address(s->varlink_server, socket, 0600); | |
2061 | else | |
2062 | r = varlink_server_listen_fd(s->varlink_server, fd); | |
1ec23479 LP |
2063 | if (r < 0) |
2064 | return r; | |
2065 | ||
2066 | r = varlink_server_attach_event(s->varlink_server, s->event, SD_EVENT_PRIORITY_NORMAL); | |
2067 | if (r < 0) | |
2068 | return r; | |
2069 | ||
2070 | return 0; | |
2071 | } | |
2072 | ||
65c398c0 LP |
2073 | static bool server_is_idle(Server *s) { |
2074 | assert(s); | |
2075 | ||
2076 | /* The server for the main namespace is never idle */ | |
2077 | if (!s->namespace) | |
2078 | return false; | |
2079 | ||
2080 | /* If a retention maximum is set larger than the idle time we need to be running to enforce it, hence | |
2081 | * turn off the idle logic. */ | |
2082 | if (s->max_retention_usec > IDLE_TIMEOUT_USEC) | |
2083 | return false; | |
2084 | ||
2085 | /* We aren't idle if we have a varlink client */ | |
2086 | if (varlink_server_current_connections(s->varlink_server) > 0) | |
2087 | return false; | |
2088 | ||
2089 | /* If we have stdout streams we aren't idle */ | |
2090 | if (s->n_stdout_streams > 0) | |
2091 | return false; | |
2092 | ||
2093 | return true; | |
2094 | } | |
2095 | ||
2096 | static int server_idle_handler(sd_event_source *source, uint64_t usec, void *userdata) { | |
2097 | Server *s = userdata; | |
2098 | ||
2099 | assert(source); | |
2100 | assert(s); | |
2101 | ||
2102 | log_debug("Server is idle, exiting."); | |
2103 | sd_event_exit(s->event, 0); | |
2104 | return 0; | |
2105 | } | |
2106 | ||
2107 | int server_start_or_stop_idle_timer(Server *s) { | |
2108 | _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL; | |
65c398c0 LP |
2109 | int r; |
2110 | ||
2111 | assert(s); | |
2112 | ||
2113 | if (!server_is_idle(s)) { | |
2114 | s->idle_event_source = sd_event_source_disable_unref(s->idle_event_source); | |
2115 | return 0; | |
2116 | } | |
2117 | ||
2118 | if (s->idle_event_source) | |
2119 | return 1; | |
2120 | ||
39cf0351 | 2121 | r = sd_event_add_time_relative(s->event, &source, CLOCK_MONOTONIC, IDLE_TIMEOUT_USEC, 0, server_idle_handler, s); |
65c398c0 LP |
2122 | if (r < 0) |
2123 | return log_error_errno(r, "Failed to allocate idle timer: %m"); | |
2124 | ||
2125 | r = sd_event_source_set_priority(source, SD_EVENT_PRIORITY_IDLE); | |
2126 | if (r < 0) | |
2127 | return log_error_errno(r, "Failed to set idle timer priority: %m"); | |
2128 | ||
2129 | (void) sd_event_source_set_description(source, "idle-timer"); | |
2130 | ||
2131 | s->idle_event_source = TAKE_PTR(source); | |
2132 | return 1; | |
2133 | } | |
2134 | ||
2135 | int server_refresh_idle_timer(Server *s) { | |
65c398c0 LP |
2136 | int r; |
2137 | ||
2138 | assert(s); | |
2139 | ||
2140 | if (!s->idle_event_source) | |
2141 | return 0; | |
2142 | ||
39cf0351 | 2143 | r = sd_event_source_set_time_relative(s->idle_event_source, IDLE_TIMEOUT_USEC); |
65c398c0 LP |
2144 | if (r < 0) |
2145 | return log_error_errno(r, "Failed to refresh idle timer: %m"); | |
2146 | ||
2147 | return 1; | |
2148 | } | |
2149 | ||
b1852c48 LP |
2150 | static int set_namespace(Server *s, const char *namespace) { |
2151 | assert(s); | |
2152 | ||
2153 | if (!namespace) | |
2154 | return 0; | |
2155 | ||
2156 | if (!log_namespace_name_valid(namespace)) | |
2157 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified namespace name not valid, refusing: %s", namespace); | |
2158 | ||
2159 | s->namespace = strdup(namespace); | |
2160 | if (!s->namespace) | |
2161 | return log_oom(); | |
2162 | ||
2163 | s->namespace_field = strjoin("_NAMESPACE=", namespace); | |
2164 | if (!s->namespace_field) | |
2165 | return log_oom(); | |
2166 | ||
2167 | return 1; | |
2168 | } | |
2169 | ||
2170 | int server_init(Server *s, const char *namespace) { | |
dc5437c7 | 2171 | const char *native_socket, *syslog_socket, *stdout_socket, *varlink_socket, *e; |
13790add | 2172 | _cleanup_fdset_free_ FDSet *fds = NULL; |
dc5437c7 | 2173 | int n, r, fd, varlink_fd = -1; |
7d18d348 | 2174 | bool no_sockets; |
d025f1e4 ZJS |
2175 | |
2176 | assert(s); | |
2177 | ||
e4d9c985 LP |
2178 | *s = (Server) { |
2179 | .syslog_fd = -1, | |
2180 | .native_fd = -1, | |
2181 | .stdout_fd = -1, | |
2182 | .dev_kmsg_fd = -1, | |
2183 | .audit_fd = -1, | |
2184 | .hostname_fd = -1, | |
2185 | .notify_fd = -1, | |
d025f1e4 | 2186 | |
e4d9c985 LP |
2187 | .compress.enabled = true, |
2188 | .compress.threshold_bytes = (uint64_t) -1, | |
2189 | .seal = true, | |
119e9655 | 2190 | |
511e03a3 LP |
2191 | .set_audit = true, |
2192 | ||
e4d9c985 LP |
2193 | .watchdog_usec = USEC_INFINITY, |
2194 | ||
2195 | .sync_interval_usec = DEFAULT_SYNC_INTERVAL_USEC, | |
2196 | .sync_scheduled = false, | |
26687bf8 | 2197 | |
5ac1530e ZJS |
2198 | .ratelimit_interval = DEFAULT_RATE_LIMIT_INTERVAL, |
2199 | .ratelimit_burst = DEFAULT_RATE_LIMIT_BURST, | |
d025f1e4 | 2200 | |
e4d9c985 | 2201 | .forward_to_wall = true, |
d025f1e4 | 2202 | |
e4d9c985 | 2203 | .max_file_usec = DEFAULT_MAX_FILE_USEC, |
e150e820 | 2204 | |
e4d9c985 LP |
2205 | .max_level_store = LOG_DEBUG, |
2206 | .max_level_syslog = LOG_DEBUG, | |
2207 | .max_level_kmsg = LOG_NOTICE, | |
2208 | .max_level_console = LOG_INFO, | |
2209 | .max_level_wall = LOG_EMERG, | |
d025f1e4 | 2210 | |
e4d9c985 LP |
2211 | .line_max = DEFAULT_LINE_MAX, |
2212 | ||
2213 | .runtime_storage.name = "Runtime Journal", | |
2214 | .system_storage.name = "System Journal", | |
2215 | }; | |
ec20fe5f | 2216 | |
b1852c48 LP |
2217 | r = set_namespace(s, namespace); |
2218 | if (r < 0) | |
2219 | return r; | |
2220 | ||
2221 | /* By default, only read from /dev/kmsg if are the main namespace */ | |
2222 | s->read_kmsg = !s->namespace; | |
2223 | s->storage = s->namespace ? STORAGE_PERSISTENT : STORAGE_AUTO; | |
2224 | ||
266a4700 FB |
2225 | journal_reset_metrics(&s->system_storage.metrics); |
2226 | journal_reset_metrics(&s->runtime_storage.metrics); | |
d025f1e4 ZJS |
2227 | |
2228 | server_parse_config_file(s); | |
1d84ad94 | 2229 | |
b1852c48 LP |
2230 | if (!s->namespace) { |
2231 | /* Parse kernel command line, but only if we are not a namespace instance */ | |
2232 | r = proc_cmdline_parse(parse_proc_cmdline_item, s, PROC_CMDLINE_STRIP_RD_PREFIX); | |
2233 | if (r < 0) | |
2234 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); | |
2235 | } | |
8580d1f7 | 2236 | |
d83f7e4c | 2237 | if (!!s->ratelimit_interval != !!s->ratelimit_burst) { /* One set to 0 and the other not? */ |
b1389b0d | 2238 | log_debug("Setting both rate limit interval and burst from "USEC_FMT",%u to 0,0", |
5ac1530e ZJS |
2239 | s->ratelimit_interval, s->ratelimit_burst); |
2240 | s->ratelimit_interval = s->ratelimit_burst = 0; | |
d288f79f | 2241 | } |
d025f1e4 | 2242 | |
b1852c48 LP |
2243 | e = getenv("RUNTIME_DIRECTORY"); |
2244 | if (e) | |
2245 | s->runtime_directory = strdup(e); | |
2246 | else if (s->namespace) | |
2247 | s->runtime_directory = strjoin("/run/systemd/journal.", s->namespace); | |
2248 | else | |
2249 | s->runtime_directory = strdup("/run/systemd/journal"); | |
2250 | if (!s->runtime_directory) | |
2251 | return log_oom(); | |
2252 | ||
2253 | (void) mkdir_p(s->runtime_directory, 0755); | |
d025f1e4 | 2254 | |
43cf8388 | 2255 | s->user_journals = ordered_hashmap_new(NULL); |
d025f1e4 ZJS |
2256 | if (!s->user_journals) |
2257 | return log_oom(); | |
2258 | ||
2259 | s->mmap = mmap_cache_new(); | |
2260 | if (!s->mmap) | |
2261 | return log_oom(); | |
2262 | ||
b58c888f VC |
2263 | s->deferred_closes = set_new(NULL); |
2264 | if (!s->deferred_closes) | |
2265 | return log_oom(); | |
2266 | ||
f9a810be | 2267 | r = sd_event_default(&s->event); |
23bbb0de MS |
2268 | if (r < 0) |
2269 | return log_error_errno(r, "Failed to create event loop: %m"); | |
d025f1e4 ZJS |
2270 | |
2271 | n = sd_listen_fds(true); | |
23bbb0de MS |
2272 | if (n < 0) |
2273 | return log_error_errno(n, "Failed to read listening file descriptors from environment: %m"); | |
d025f1e4 | 2274 | |
b1852c48 LP |
2275 | native_socket = strjoina(s->runtime_directory, "/socket"); |
2276 | stdout_socket = strjoina(s->runtime_directory, "/stdout"); | |
2277 | syslog_socket = strjoina(s->runtime_directory, "/dev-log"); | |
dc5437c7 | 2278 | varlink_socket = strjoina(s->runtime_directory, "/io.systemd.journal"); |
b1852c48 | 2279 | |
d025f1e4 ZJS |
2280 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { |
2281 | ||
b1852c48 | 2282 | if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, native_socket, 0) > 0) { |
d025f1e4 | 2283 | |
baaa35ad ZJS |
2284 | if (s->native_fd >= 0) |
2285 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2286 | "Too many native sockets passed."); | |
d025f1e4 ZJS |
2287 | |
2288 | s->native_fd = fd; | |
2289 | ||
b1852c48 | 2290 | } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, stdout_socket, 0) > 0) { |
d025f1e4 | 2291 | |
baaa35ad ZJS |
2292 | if (s->stdout_fd >= 0) |
2293 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2294 | "Too many stdout sockets passed."); | |
d025f1e4 ZJS |
2295 | |
2296 | s->stdout_fd = fd; | |
2297 | ||
b1852c48 | 2298 | } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, syslog_socket, 0) > 0) { |
d025f1e4 | 2299 | |
baaa35ad ZJS |
2300 | if (s->syslog_fd >= 0) |
2301 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2302 | "Too many /dev/log sockets passed."); | |
d025f1e4 ZJS |
2303 | |
2304 | s->syslog_fd = fd; | |
2305 | ||
dc5437c7 LP |
2306 | } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, varlink_socket, 0) > 0) { |
2307 | ||
2308 | if (varlink_fd >= 0) | |
2309 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2310 | "Too many varlink sockets passed."); | |
2311 | ||
2312 | varlink_fd = fd; | |
875c2e22 LP |
2313 | } else if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { |
2314 | ||
baaa35ad ZJS |
2315 | if (s->audit_fd >= 0) |
2316 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2317 | "Too many audit sockets passed."); | |
875c2e22 LP |
2318 | |
2319 | s->audit_fd = fd; | |
2320 | ||
4ec3cd73 | 2321 | } else { |
4ec3cd73 | 2322 | |
13790add LP |
2323 | if (!fds) { |
2324 | fds = fdset_new(); | |
2325 | if (!fds) | |
2326 | return log_oom(); | |
2327 | } | |
4ec3cd73 | 2328 | |
13790add LP |
2329 | r = fdset_put(fds, fd); |
2330 | if (r < 0) | |
2331 | return log_oom(); | |
4ec3cd73 | 2332 | } |
d025f1e4 ZJS |
2333 | } |
2334 | ||
15d91bff ZJS |
2335 | /* Try to restore streams, but don't bother if this fails */ |
2336 | (void) server_restore_streams(s, fds); | |
d025f1e4 | 2337 | |
13790add LP |
2338 | if (fdset_size(fds) > 0) { |
2339 | log_warning("%u unknown file descriptors passed, closing.", fdset_size(fds)); | |
2340 | fds = fdset_free(fds); | |
2341 | } | |
2342 | ||
dc5437c7 | 2343 | no_sockets = s->native_fd < 0 && s->stdout_fd < 0 && s->syslog_fd < 0 && s->audit_fd < 0 && varlink_fd < 0; |
7d18d348 ZJS |
2344 | |
2345 | /* always open stdout, syslog, native, and kmsg sockets */ | |
37b7affe ZJS |
2346 | |
2347 | /* systemd-journald.socket: /run/systemd/journal/stdout */ | |
b1852c48 | 2348 | r = server_open_stdout_socket(s, stdout_socket); |
15d91bff ZJS |
2349 | if (r < 0) |
2350 | return r; | |
2351 | ||
37b7affe | 2352 | /* systemd-journald-dev-log.socket: /run/systemd/journal/dev-log */ |
b1852c48 | 2353 | r = server_open_syslog_socket(s, syslog_socket); |
d025f1e4 ZJS |
2354 | if (r < 0) |
2355 | return r; | |
2356 | ||
37b7affe | 2357 | /* systemd-journald.socket: /run/systemd/journal/socket */ |
b1852c48 | 2358 | r = server_open_native_socket(s, native_socket); |
d025f1e4 ZJS |
2359 | if (r < 0) |
2360 | return r; | |
2361 | ||
b2392ff3 | 2362 | /* /dev/kmsg */ |
d025f1e4 ZJS |
2363 | r = server_open_dev_kmsg(s); |
2364 | if (r < 0) | |
2365 | return r; | |
2366 | ||
7d18d348 ZJS |
2367 | /* Unless we got *some* sockets and not audit, open audit socket */ |
2368 | if (s->audit_fd >= 0 || no_sockets) { | |
2369 | r = server_open_audit(s); | |
2370 | if (r < 0) | |
2371 | return r; | |
2372 | } | |
875c2e22 | 2373 | |
dc5437c7 | 2374 | r = server_open_varlink(s, varlink_socket, varlink_fd); |
1ec23479 LP |
2375 | if (r < 0) |
2376 | return r; | |
2377 | ||
d025f1e4 ZJS |
2378 | r = server_open_kernel_seqnum(s); |
2379 | if (r < 0) | |
2380 | return r; | |
2381 | ||
0c24bb23 LP |
2382 | r = server_open_hostname(s); |
2383 | if (r < 0) | |
2384 | return r; | |
2385 | ||
f9a810be | 2386 | r = setup_signals(s); |
d025f1e4 ZJS |
2387 | if (r < 0) |
2388 | return r; | |
2389 | ||
5ac1530e ZJS |
2390 | s->ratelimit = journal_ratelimit_new(); |
2391 | if (!s->ratelimit) | |
659a77be | 2392 | return log_oom(); |
d025f1e4 | 2393 | |
e9174f29 LP |
2394 | r = cg_get_root_path(&s->cgroup_root); |
2395 | if (r < 0) | |
659a77be | 2396 | return log_error_errno(r, "Failed to acquire cgroup root path: %m"); |
e9174f29 | 2397 | |
0c24bb23 LP |
2398 | server_cache_hostname(s); |
2399 | server_cache_boot_id(s); | |
2400 | server_cache_machine_id(s); | |
2401 | ||
b1852c48 LP |
2402 | if (s->namespace) |
2403 | s->runtime_storage.path = strjoin("/run/log/journal/", SERVER_MACHINE_ID(s), ".", s->namespace); | |
2404 | else | |
2405 | s->runtime_storage.path = strjoin("/run/log/journal/", SERVER_MACHINE_ID(s)); | |
2406 | if (!s->runtime_storage.path) | |
2407 | return log_oom(); | |
2408 | ||
2409 | e = getenv("LOGS_DIRECTORY"); | |
2410 | if (e) | |
2411 | s->system_storage.path = strdup(e); | |
2412 | else if (s->namespace) | |
2413 | s->system_storage.path = strjoin("/var/log/journal/", SERVER_MACHINE_ID(s), ".", s->namespace); | |
2414 | else | |
2415 | s->system_storage.path = strjoin("/var/log/journal/", SERVER_MACHINE_ID(s)); | |
2416 | if (!s->system_storage.path) | |
659a77be | 2417 | return log_oom(); |
266a4700 | 2418 | |
e22aa3d3 LP |
2419 | (void) server_connect_notify(s); |
2420 | ||
22e3a02b LP |
2421 | (void) client_context_acquire_default(s); |
2422 | ||
65c398c0 LP |
2423 | r = system_journal_open(s, false, false); |
2424 | if (r < 0) | |
2425 | return r; | |
2426 | ||
2427 | server_start_or_stop_idle_timer(s); | |
2428 | return 0; | |
d025f1e4 ZJS |
2429 | } |
2430 | ||
2431 | void server_maybe_append_tags(Server *s) { | |
349cc4a5 | 2432 | #if HAVE_GCRYPT |
d025f1e4 | 2433 | JournalFile *f; |
d025f1e4 ZJS |
2434 | usec_t n; |
2435 | ||
2436 | n = now(CLOCK_REALTIME); | |
2437 | ||
2438 | if (s->system_journal) | |
2439 | journal_file_maybe_append_tag(s->system_journal, n); | |
2440 | ||
90e74a66 | 2441 | ORDERED_HASHMAP_FOREACH(f, s->user_journals) |
d025f1e4 ZJS |
2442 | journal_file_maybe_append_tag(f, n); |
2443 | #endif | |
2444 | } | |
2445 | ||
2446 | void server_done(Server *s) { | |
d025f1e4 ZJS |
2447 | assert(s); |
2448 | ||
b1852c48 LP |
2449 | free(s->namespace); |
2450 | free(s->namespace_field); | |
2451 | ||
f9168190 | 2452 | set_free_with_destructor(s->deferred_closes, journal_file_close); |
b58c888f | 2453 | |
d025f1e4 ZJS |
2454 | while (s->stdout_streams) |
2455 | stdout_stream_free(s->stdout_streams); | |
2456 | ||
22e3a02b LP |
2457 | client_context_flush_all(s); |
2458 | ||
c377a6f3 YW |
2459 | (void) journal_file_close(s->system_journal); |
2460 | (void) journal_file_close(s->runtime_journal); | |
d025f1e4 | 2461 | |
f9168190 | 2462 | ordered_hashmap_free_with_destructor(s->user_journals, journal_file_close); |
d025f1e4 | 2463 | |
1ec23479 LP |
2464 | varlink_server_unref(s->varlink_server); |
2465 | ||
f9a810be LP |
2466 | sd_event_source_unref(s->syslog_event_source); |
2467 | sd_event_source_unref(s->native_event_source); | |
2468 | sd_event_source_unref(s->stdout_event_source); | |
2469 | sd_event_source_unref(s->dev_kmsg_event_source); | |
875c2e22 | 2470 | sd_event_source_unref(s->audit_event_source); |
f9a810be LP |
2471 | sd_event_source_unref(s->sync_event_source); |
2472 | sd_event_source_unref(s->sigusr1_event_source); | |
2473 | sd_event_source_unref(s->sigusr2_event_source); | |
2474 | sd_event_source_unref(s->sigterm_event_source); | |
2475 | sd_event_source_unref(s->sigint_event_source); | |
94b65516 | 2476 | sd_event_source_unref(s->sigrtmin1_event_source); |
0c24bb23 | 2477 | sd_event_source_unref(s->hostname_event_source); |
e22aa3d3 | 2478 | sd_event_source_unref(s->notify_event_source); |
119e9655 | 2479 | sd_event_source_unref(s->watchdog_event_source); |
65c398c0 | 2480 | sd_event_source_unref(s->idle_event_source); |
f9a810be | 2481 | sd_event_unref(s->event); |
d025f1e4 | 2482 | |
03e334a1 LP |
2483 | safe_close(s->syslog_fd); |
2484 | safe_close(s->native_fd); | |
2485 | safe_close(s->stdout_fd); | |
2486 | safe_close(s->dev_kmsg_fd); | |
875c2e22 | 2487 | safe_close(s->audit_fd); |
03e334a1 | 2488 | safe_close(s->hostname_fd); |
e22aa3d3 | 2489 | safe_close(s->notify_fd); |
0c24bb23 | 2490 | |
5ac1530e ZJS |
2491 | if (s->ratelimit) |
2492 | journal_ratelimit_free(s->ratelimit); | |
d025f1e4 ZJS |
2493 | |
2494 | if (s->kernel_seqnum) | |
2495 | munmap(s->kernel_seqnum, sizeof(uint64_t)); | |
2496 | ||
2497 | free(s->buffer); | |
2498 | free(s->tty_path); | |
e9174f29 | 2499 | free(s->cgroup_root); |
99d0966e | 2500 | free(s->hostname_field); |
c6e9e16f ZJS |
2501 | free(s->runtime_storage.path); |
2502 | free(s->system_storage.path); | |
b1852c48 | 2503 | free(s->runtime_directory); |
d025f1e4 | 2504 | |
e3d78cb1 | 2505 | mmap_cache_unref(s->mmap); |
d025f1e4 | 2506 | } |
8580d1f7 LP |
2507 | |
2508 | static const char* const storage_table[_STORAGE_MAX] = { | |
2509 | [STORAGE_AUTO] = "auto", | |
2510 | [STORAGE_VOLATILE] = "volatile", | |
2511 | [STORAGE_PERSISTENT] = "persistent", | |
2512 | [STORAGE_NONE] = "none" | |
2513 | }; | |
2514 | ||
2515 | DEFINE_STRING_TABLE_LOOKUP(storage, Storage); | |
2516 | DEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting"); | |
2517 | ||
2518 | static const char* const split_mode_table[_SPLIT_MAX] = { | |
2519 | [SPLIT_LOGIN] = "login", | |
2520 | [SPLIT_UID] = "uid", | |
2521 | [SPLIT_NONE] = "none", | |
2522 | }; | |
2523 | ||
2524 | DEFINE_STRING_TABLE_LOOKUP(split_mode, SplitMode); | |
2525 | DEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode, split_mode, SplitMode, "Failed to parse split mode setting"); | |
ec20fe5f LP |
2526 | |
2527 | int config_parse_line_max( | |
2528 | const char* unit, | |
2529 | const char *filename, | |
2530 | unsigned line, | |
2531 | const char *section, | |
2532 | unsigned section_line, | |
2533 | const char *lvalue, | |
2534 | int ltype, | |
2535 | const char *rvalue, | |
2536 | void *data, | |
2537 | void *userdata) { | |
2538 | ||
2539 | size_t *sz = data; | |
2540 | int r; | |
2541 | ||
2542 | assert(filename); | |
2543 | assert(lvalue); | |
2544 | assert(rvalue); | |
2545 | assert(data); | |
2546 | ||
2547 | if (isempty(rvalue)) | |
2548 | /* Empty assignment means default */ | |
2549 | *sz = DEFAULT_LINE_MAX; | |
2550 | else { | |
2551 | uint64_t v; | |
2552 | ||
2553 | r = parse_size(rvalue, 1024, &v); | |
2554 | if (r < 0) { | |
adb58487 | 2555 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse LineMax= value, ignoring: %s", rvalue); |
ec20fe5f LP |
2556 | return 0; |
2557 | } | |
2558 | ||
2559 | if (v < 79) { | |
2560 | /* Why specify 79 here as minimum line length? Simply, because the most common traditional | |
2561 | * terminal size is 80ch, and it might make sense to break one character before the natural | |
2562 | * line break would occur on that. */ | |
2563 | log_syntax(unit, LOG_WARNING, filename, line, 0, "LineMax= too small, clamping to 79: %s", rvalue); | |
2564 | *sz = 79; | |
2565 | } else if (v > (uint64_t) (SSIZE_MAX-1)) { | |
2566 | /* So, why specify SSIZE_MAX-1 here? Because that's one below the largest size value read() | |
2567 | * can return, and we need one extra byte for the trailing NUL byte. Of course IRL such large | |
2568 | * memory allocations will fail anyway, hence this limit is mostly theoretical anyway, as we'll | |
2569 | * fail much earlier anyway. */ | |
2570 | log_syntax(unit, LOG_WARNING, filename, line, 0, "LineMax= too large, clamping to %" PRIu64 ": %s", (uint64_t) (SSIZE_MAX-1), rvalue); | |
2571 | *sz = SSIZE_MAX-1; | |
2572 | } else | |
2573 | *sz = (size_t) v; | |
2574 | } | |
2575 | ||
2576 | return 0; | |
2577 | } | |
1b7cf0e5 | 2578 | |
e3d36a8d LP |
2579 | int config_parse_compress( |
2580 | const char* unit, | |
2581 | const char *filename, | |
2582 | unsigned line, | |
2583 | const char *section, | |
2584 | unsigned section_line, | |
2585 | const char *lvalue, | |
2586 | int ltype, | |
2587 | const char *rvalue, | |
2588 | void *data, | |
2589 | void *userdata) { | |
2590 | ||
1b7cf0e5 AG |
2591 | JournalCompressOptions* compress = data; |
2592 | int r; | |
2593 | ||
e3d36a8d LP |
2594 | if (isempty(rvalue)) { |
2595 | compress->enabled = true; | |
2596 | compress->threshold_bytes = (uint64_t) -1; | |
2597 | } else if (streq(rvalue, "1")) { | |
1b7cf0e5 AG |
2598 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
2599 | "Compress= ambiguously specified as 1, enabling compression with default threshold"); | |
2600 | compress->enabled = true; | |
2601 | } else if (streq(rvalue, "0")) { | |
2602 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
2603 | "Compress= ambiguously specified as 0, disabling compression"); | |
2604 | compress->enabled = false; | |
e3d36a8d LP |
2605 | } else { |
2606 | r = parse_boolean(rvalue); | |
2607 | if (r < 0) { | |
2608 | r = parse_size(rvalue, 1024, &compress->threshold_bytes); | |
2609 | if (r < 0) | |
adb58487 | 2610 | log_syntax(unit, LOG_WARNING, filename, line, r, |
e3d36a8d LP |
2611 | "Failed to parse Compress= value, ignoring: %s", rvalue); |
2612 | else | |
2613 | compress->enabled = true; | |
2614 | } else | |
2615 | compress->enabled = r; | |
2616 | } | |
1b7cf0e5 AG |
2617 | |
2618 | return 0; | |
2619 | } |