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