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