]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald-server.c
util-lib: split out globbing related calls into glob-util.[ch]
[thirdparty/systemd.git] / src / journal / journald-server.c
CommitLineData
d025f1e4
ZJS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
24882e06
LP
22#ifdef HAVE_SELINUX
23#include <selinux/selinux.h>
24#endif
8580d1f7
LP
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <sys/signalfd.h>
28#include <sys/statvfs.h>
07630cea 29#include <linux/sockios.h>
24882e06 30
8580d1f7
LP
31#include "libudev.h"
32#include "sd-daemon.h"
74df0fca
LP
33#include "sd-journal.h"
34#include "sd-messages.h"
8580d1f7
LP
35
36#include "acl-util.h"
430f0182 37#include "audit-util.h"
d025f1e4 38#include "cgroup-util.h"
d025f1e4 39#include "conf-parser.h"
a0956174 40#include "dirent-util.h"
0dec689b 41#include "extract-word.h"
3ffd4af2 42#include "fd-util.h"
958b66ea 43#include "formats-util.h"
f4f15635 44#include "fs-util.h"
8580d1f7 45#include "hashmap.h"
958b66ea 46#include "hostname-util.h"
afc5dbf3 47#include "io-util.h"
8580d1f7
LP
48#include "journal-authenticate.h"
49#include "journal-file.h"
d025f1e4
ZJS
50#include "journal-internal.h"
51#include "journal-vacuum.h"
8580d1f7 52#include "journald-audit.h"
d025f1e4 53#include "journald-kmsg.h"
d025f1e4 54#include "journald-native.h"
8580d1f7 55#include "journald-rate-limit.h"
3ffd4af2 56#include "journald-server.h"
8580d1f7
LP
57#include "journald-stream.h"
58#include "journald-syslog.h"
07630cea
LP
59#include "missing.h"
60#include "mkdir.h"
6bedfcbb 61#include "parse-util.h"
4e731273 62#include "proc-cmdline.h"
07630cea
LP
63#include "process-util.h"
64#include "rm-rf.h"
65#include "selinux-util.h"
66#include "signal-util.h"
67#include "socket-util.h"
8b43440b 68#include "string-table.h"
07630cea 69#include "string-util.h"
d025f1e4 70
d025f1e4
ZJS
71#define USER_JOURNALS_MAX 1024
72
26687bf8 73#define DEFAULT_SYNC_INTERVAL_USEC (5*USEC_PER_MINUTE)
7f1ad696
LP
74#define DEFAULT_RATE_LIMIT_INTERVAL (30*USEC_PER_SEC)
75#define DEFAULT_RATE_LIMIT_BURST 1000
e150e820 76#define DEFAULT_MAX_FILE_USEC USEC_PER_MONTH
d025f1e4 77
8580d1f7 78#define RECHECK_SPACE_USEC (30*USEC_PER_SEC)
d025f1e4 79
8580d1f7
LP
80static int determine_space_for(
81 Server *s,
82 JournalMetrics *metrics,
83 const char *path,
84 const char *name,
85 bool verbose,
86 bool patch_min_use,
87 uint64_t *available,
88 uint64_t *limit) {
89
90 uint64_t sum = 0, ss_avail, avail;
7fd1b19b 91 _cleanup_closedir_ DIR *d = NULL;
8580d1f7
LP
92 struct dirent *de;
93 struct statvfs ss;
94 const char *p;
d025f1e4 95 usec_t ts;
d025f1e4 96
8580d1f7
LP
97 assert(s);
98 assert(metrics);
99 assert(path);
100 assert(name);
d025f1e4 101
8580d1f7 102 ts = now(CLOCK_MONOTONIC);
d025f1e4 103
8580d1f7 104 if (!verbose && s->cached_space_timestamp + RECHECK_SPACE_USEC > ts) {
d025f1e4 105
8580d1f7
LP
106 if (available)
107 *available = s->cached_space_available;
108 if (limit)
109 *limit = s->cached_space_limit;
d025f1e4 110
d025f1e4 111 return 0;
8580d1f7 112 }
d025f1e4 113
8580d1f7 114 p = strjoina(path, SERVER_MACHINE_ID(s));
d025f1e4 115 d = opendir(p);
d025f1e4 116 if (!d)
8580d1f7 117 return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to open %s: %m", p);
d025f1e4
ZJS
118
119 if (fstatvfs(dirfd(d), &ss) < 0)
8580d1f7 120 return log_error_errno(errno, "Failed to fstatvfs(%s): %m", p);
d025f1e4 121
8580d1f7 122 FOREACH_DIRENT_ALL(de, d, break) {
d025f1e4 123 struct stat st;
d025f1e4
ZJS
124
125 if (!endswith(de->d_name, ".journal") &&
126 !endswith(de->d_name, ".journal~"))
127 continue;
128
8580d1f7
LP
129 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
130 log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", p, de->d_name);
d025f1e4 131 continue;
8580d1f7 132 }
d025f1e4
ZJS
133
134 if (!S_ISREG(st.st_mode))
135 continue;
136
137 sum += (uint64_t) st.st_blocks * 512UL;
138 }
139
8580d1f7
LP
140 /* If request, then let's bump the min_use limit to the
141 * current usage on disk. We do this when starting up and
142 * first opening the journal files. This way sudden spikes in
143 * disk usage will not cause journald to vacuum files without
144 * bounds. Note that this means that only a restart of
145 * journald will make it reset this value. */
d025f1e4 146
8580d1f7
LP
147 if (patch_min_use)
148 metrics->min_use = MAX(metrics->min_use, sum);
348ced90 149
8580d1f7
LP
150 ss_avail = ss.f_bsize * ss.f_bavail;
151 avail = LESS_BY(ss_avail, metrics->keep_free);
348ced90 152
8580d1f7
LP
153 s->cached_space_limit = MIN(MAX(sum + avail, metrics->min_use), metrics->max_use);
154 s->cached_space_available = LESS_BY(s->cached_space_limit, sum);
155 s->cached_space_timestamp = ts;
d025f1e4 156
670b110c
ZJS
157 if (verbose) {
158 char fb1[FORMAT_BYTES_MAX], fb2[FORMAT_BYTES_MAX], fb3[FORMAT_BYTES_MAX],
8580d1f7 159 fb4[FORMAT_BYTES_MAX], fb5[FORMAT_BYTES_MAX], fb6[FORMAT_BYTES_MAX];
670b110c
ZJS
160
161 server_driver_message(s, SD_MESSAGE_JOURNAL_USAGE,
8580d1f7 162 "%s (%s) is currently using %s.\n"
da2e288b
LP
163 "Maximum allowed usage is set to %s.\n"
164 "Leaving at least %s free (of currently available %s of space).\n"
8580d1f7
LP
165 "Enforced usage limit is thus %s, of which %s are still available.",
166 name, path,
670b110c 167 format_bytes(fb1, sizeof(fb1), sum),
8580d1f7
LP
168 format_bytes(fb2, sizeof(fb2), metrics->max_use),
169 format_bytes(fb3, sizeof(fb3), metrics->keep_free),
670b110c 170 format_bytes(fb4, sizeof(fb4), ss_avail),
8580d1f7
LP
171 format_bytes(fb5, sizeof(fb5), s->cached_space_limit),
172 format_bytes(fb6, sizeof(fb6), s->cached_space_available));
173 }
174
175 if (available)
176 *available = s->cached_space_available;
177 if (limit)
178 *limit = s->cached_space_limit;
179
180 return 1;
181}
182
183static int determine_space(Server *s, bool verbose, bool patch_min_use, uint64_t *available, uint64_t *limit) {
184 JournalMetrics *metrics;
185 const char *path, *name;
186
187 assert(s);
188
189 if (s->system_journal) {
190 path = "/var/log/journal/";
191 metrics = &s->system_metrics;
192 name = "System journal";
193 } else {
194 path = "/run/log/journal/";
195 metrics = &s->runtime_metrics;
196 name = "Runtime journal";
670b110c
ZJS
197 }
198
8580d1f7 199 return determine_space_for(s, metrics, path, name, verbose, patch_min_use, available, limit);
d025f1e4
ZJS
200}
201
d025f1e4
ZJS
202void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
203 int r;
204#ifdef HAVE_ACL
0fb39831 205 _cleanup_(acl_freep) acl_t acl = NULL;
d025f1e4
ZJS
206 acl_entry_t entry;
207 acl_permset_t permset;
208#endif
209
210 assert(f);
211
4608af43 212 r = fchmod(f->fd, 0640);
d025f1e4 213 if (r < 0)
65089b82 214 log_warning_errno(errno, "Failed to fix access mode on %s, ignoring: %m", f->path);
d025f1e4
ZJS
215
216#ifdef HAVE_ACL
34c10968 217 if (uid <= SYSTEM_UID_MAX)
d025f1e4
ZJS
218 return;
219
220 acl = acl_get_fd(f->fd);
221 if (!acl) {
56f64d95 222 log_warning_errno(errno, "Failed to read ACL on %s, ignoring: %m", f->path);
d025f1e4
ZJS
223 return;
224 }
225
226 r = acl_find_uid(acl, uid, &entry);
227 if (r <= 0) {
228
229 if (acl_create_entry(&acl, &entry) < 0 ||
230 acl_set_tag_type(entry, ACL_USER) < 0 ||
231 acl_set_qualifier(entry, &uid) < 0) {
56f64d95 232 log_warning_errno(errno, "Failed to patch ACL on %s, ignoring: %m", f->path);
0fb39831 233 return;
d025f1e4
ZJS
234 }
235 }
236
23ad4dd8
JAS
237 /* We do not recalculate the mask unconditionally here,
238 * so that the fchmod() mask above stays intact. */
d025f1e4 239 if (acl_get_permset(entry, &permset) < 0 ||
23ad4dd8
JAS
240 acl_add_perm(permset, ACL_READ) < 0 ||
241 calc_acl_mask_if_needed(&acl) < 0) {
56f64d95 242 log_warning_errno(errno, "Failed to patch ACL on %s, ignoring: %m", f->path);
0fb39831 243 return;
d025f1e4
ZJS
244 }
245
246 if (acl_set_fd(f->fd, acl) < 0)
56f64d95 247 log_warning_errno(errno, "Failed to set ACL on %s, ignoring: %m", f->path);
d025f1e4 248
d025f1e4
ZJS
249#endif
250}
251
252static JournalFile* find_journal(Server *s, uid_t uid) {
ed375beb 253 _cleanup_free_ char *p = NULL;
d025f1e4
ZJS
254 int r;
255 JournalFile *f;
256 sd_id128_t machine;
257
258 assert(s);
259
260 /* We split up user logs only on /var, not on /run. If the
261 * runtime file is open, we write to it exclusively, in order
262 * to guarantee proper order as soon as we flush /run to
263 * /var and close the runtime file. */
264
265 if (s->runtime_journal)
266 return s->runtime_journal;
267
f7dc3ab9 268 if (uid <= SYSTEM_UID_MAX)
d025f1e4
ZJS
269 return s->system_journal;
270
271 r = sd_id128_get_machine(&machine);
272 if (r < 0)
273 return s->system_journal;
274
43cf8388 275 f = ordered_hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
d025f1e4
ZJS
276 if (f)
277 return f;
278
de0671ee
ZJS
279 if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/user-"UID_FMT".journal",
280 SD_ID128_FORMAT_VAL(machine), uid) < 0)
d025f1e4
ZJS
281 return s->system_journal;
282
43cf8388 283 while (ordered_hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
d025f1e4 284 /* Too many open? Then let's close one */
43cf8388 285 f = ordered_hashmap_steal_first(s->user_journals);
d025f1e4
ZJS
286 assert(f);
287 journal_file_close(f);
288 }
289
cbd67177 290 r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &f);
d025f1e4
ZJS
291 if (r < 0)
292 return s->system_journal;
293
294 server_fix_perms(s, f, uid);
295
43cf8388 296 r = ordered_hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
d025f1e4
ZJS
297 if (r < 0) {
298 journal_file_close(f);
299 return s->system_journal;
300 }
301
302 return f;
303}
304
ea69bd41
LP
305static int do_rotate(
306 Server *s,
307 JournalFile **f,
308 const char* name,
309 bool seal,
310 uint32_t uid) {
311
fc55baee
ZJS
312 int r;
313 assert(s);
314
315 if (!*f)
316 return -EINVAL;
317
318 r = journal_file_rotate(f, s->compress, seal);
319 if (r < 0)
320 if (*f)
ea69bd41 321 log_error_errno(r, "Failed to rotate %s: %m", (*f)->path);
fc55baee 322 else
ea69bd41 323 log_error_errno(r, "Failed to create new %s journal: %m", name);
fc55baee
ZJS
324 else
325 server_fix_perms(s, *f, uid);
2678031a 326
fc55baee
ZJS
327 return r;
328}
329
d025f1e4
ZJS
330void server_rotate(Server *s) {
331 JournalFile *f;
332 void *k;
333 Iterator i;
334 int r;
335
336 log_debug("Rotating...");
337
8580d1f7
LP
338 (void) do_rotate(s, &s->runtime_journal, "runtime", false, 0);
339 (void) do_rotate(s, &s->system_journal, "system", s->seal, 0);
d025f1e4 340
43cf8388 341 ORDERED_HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
fc55baee
ZJS
342 r = do_rotate(s, &f, "user", s->seal, PTR_TO_UINT32(k));
343 if (r >= 0)
43cf8388 344 ordered_hashmap_replace(s->user_journals, k, f);
fc55baee
ZJS
345 else if (!f)
346 /* Old file has been closed and deallocated */
43cf8388 347 ordered_hashmap_remove(s->user_journals, k);
d025f1e4
ZJS
348 }
349}
350
26687bf8
OS
351void server_sync(Server *s) {
352 JournalFile *f;
353 void *k;
354 Iterator i;
355 int r;
356
26687bf8
OS
357 if (s->system_journal) {
358 r = journal_file_set_offline(s->system_journal);
359 if (r < 0)
65089b82 360 log_warning_errno(r, "Failed to sync system journal, ignoring: %m");
26687bf8
OS
361 }
362
43cf8388 363 ORDERED_HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
26687bf8
OS
364 r = journal_file_set_offline(f);
365 if (r < 0)
65089b82 366 log_warning_errno(r, "Failed to sync user journal, ignoring: %m");
26687bf8
OS
367 }
368
f9a810be
LP
369 if (s->sync_event_source) {
370 r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_OFF);
371 if (r < 0)
da927ba9 372 log_error_errno(r, "Failed to disable sync timer source: %m");
f9a810be 373 }
26687bf8
OS
374
375 s->sync_scheduled = false;
376}
377
ea69bd41
LP
378static void do_vacuum(
379 Server *s,
ea69bd41 380 JournalFile *f,
8580d1f7
LP
381 JournalMetrics *metrics,
382 const char *path,
383 const char *name,
384 bool verbose,
385 bool patch_min_use) {
ea69bd41
LP
386
387 const char *p;
8580d1f7 388 uint64_t limit;
63c8666b
ZJS
389 int r;
390
8580d1f7
LP
391 assert(s);
392 assert(metrics);
393 assert(path);
394 assert(name);
395
63c8666b
ZJS
396 if (!f)
397 return;
398
8580d1f7
LP
399 p = strjoina(path, SERVER_MACHINE_ID(s));
400
401 limit = metrics->max_use;
402 (void) determine_space_for(s, metrics, path, name, verbose, patch_min_use, NULL, &limit);
403
404 r = journal_directory_vacuum(p, limit, metrics->n_max_files, s->max_retention_usec, &s->oldest_file_usec, verbose);
63c8666b 405 if (r < 0 && r != -ENOENT)
8580d1f7 406 log_warning_errno(r, "Failed to vacuum %s, ignoring: %m", p);
63c8666b
ZJS
407}
408
8580d1f7
LP
409int server_vacuum(Server *s, bool verbose, bool patch_min_use) {
410 assert(s);
d025f1e4
ZJS
411
412 log_debug("Vacuuming...");
413
414 s->oldest_file_usec = 0;
415
8580d1f7
LP
416 do_vacuum(s, s->system_journal, &s->system_metrics, "/var/log/journal/", "System journal", verbose, patch_min_use);
417 do_vacuum(s, s->runtime_journal, &s->runtime_metrics, "/run/log/journal/", "Runtime journal", verbose, patch_min_use);
d025f1e4 418
8580d1f7
LP
419 s->cached_space_limit = 0;
420 s->cached_space_available = 0;
421 s->cached_space_timestamp = 0;
d025f1e4 422
8580d1f7 423 return 0;
d025f1e4
ZJS
424}
425
0c24bb23
LP
426static void server_cache_machine_id(Server *s) {
427 sd_id128_t id;
428 int r;
429
430 assert(s);
431
432 r = sd_id128_get_machine(&id);
433 if (r < 0)
434 return;
435
436 sd_id128_to_string(id, stpcpy(s->machine_id_field, "_MACHINE_ID="));
437}
438
439static void server_cache_boot_id(Server *s) {
440 sd_id128_t id;
441 int r;
442
443 assert(s);
444
445 r = sd_id128_get_boot(&id);
446 if (r < 0)
447 return;
448
449 sd_id128_to_string(id, stpcpy(s->boot_id_field, "_BOOT_ID="));
450}
451
452static void server_cache_hostname(Server *s) {
453 _cleanup_free_ char *t = NULL;
454 char *x;
455
456 assert(s);
457
458 t = gethostname_malloc();
459 if (!t)
460 return;
461
462 x = strappend("_HOSTNAME=", t);
463 if (!x)
464 return;
465
466 free(s->hostname_field);
467 s->hostname_field = x;
468}
469
8531ae70 470static bool shall_try_append_again(JournalFile *f, int r) {
d025f1e4
ZJS
471
472 /* -E2BIG Hit configured limit
473 -EFBIG Hit fs limit
474 -EDQUOT Quota limit hit
475 -ENOSPC Disk full
fa6ac760 476 -EIO I/O error of some kind (mmap)
d025f1e4
ZJS
477 -EHOSTDOWN Other machine
478 -EBUSY Unclean shutdown
479 -EPROTONOSUPPORT Unsupported feature
480 -EBADMSG Corrupted
481 -ENODATA Truncated
2678031a
LP
482 -ESHUTDOWN Already archived
483 -EIDRM Journal file has been deleted */
d025f1e4
ZJS
484
485 if (r == -E2BIG || r == -EFBIG || r == -EDQUOT || r == -ENOSPC)
486 log_debug("%s: Allocation limit reached, rotating.", f->path);
487 else if (r == -EHOSTDOWN)
488 log_info("%s: Journal file from other machine, rotating.", f->path);
489 else if (r == -EBUSY)
490 log_info("%s: Unclean shutdown, rotating.", f->path);
491 else if (r == -EPROTONOSUPPORT)
492 log_info("%s: Unsupported feature, rotating.", f->path);
493 else if (r == -EBADMSG || r == -ENODATA || r == ESHUTDOWN)
494 log_warning("%s: Journal file corrupted, rotating.", f->path);
fa6ac760
LP
495 else if (r == -EIO)
496 log_warning("%s: IO error, rotating.", f->path);
2678031a
LP
497 else if (r == -EIDRM)
498 log_warning("%s: Journal file has been deleted, rotating.", f->path);
d025f1e4
ZJS
499 else
500 return false;
501
502 return true;
503}
504
d07f7b9e 505static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n, int priority) {
d025f1e4
ZJS
506 JournalFile *f;
507 bool vacuumed = false;
508 int r;
509
510 assert(s);
511 assert(iovec);
512 assert(n > 0);
513
514 f = find_journal(s, uid);
515 if (!f)
516 return;
517
518 if (journal_file_rotate_suggested(f, s->max_file_usec)) {
519 log_debug("%s: Journal header limits reached or header out-of-date, rotating.", f->path);
520 server_rotate(s);
8580d1f7 521 server_vacuum(s, false, false);
d025f1e4
ZJS
522 vacuumed = true;
523
524 f = find_journal(s, uid);
525 if (!f)
526 return;
527 }
528
529 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
26687bf8 530 if (r >= 0) {
d07f7b9e 531 server_schedule_sync(s, priority);
d025f1e4 532 return;
26687bf8 533 }
d025f1e4
ZJS
534
535 if (vacuumed || !shall_try_append_again(f, r)) {
8266e1c0 536 log_error_errno(r, "Failed to write entry (%d items, %zu bytes), ignoring: %m", n, IOVEC_TOTAL_SIZE(iovec, n));
d025f1e4
ZJS
537 return;
538 }
539
540 server_rotate(s);
8580d1f7 541 server_vacuum(s, false, false);
d025f1e4
ZJS
542
543 f = find_journal(s, uid);
544 if (!f)
545 return;
546
547 log_debug("Retrying write.");
548 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
8266e1c0
LP
549 if (r < 0)
550 log_error_errno(r, "Failed to write entry (%d items, %zu bytes) despite vacuuming, ignoring: %m", n, IOVEC_TOTAL_SIZE(iovec, n));
551 else
d07f7b9e 552 server_schedule_sync(s, priority);
d025f1e4
ZJS
553}
554
555static void dispatch_message_real(
556 Server *s,
557 struct iovec *iovec, unsigned n, unsigned m,
3b3154df
LP
558 const struct ucred *ucred,
559 const struct timeval *tv,
d025f1e4 560 const char *label, size_t label_len,
968f3196 561 const char *unit_id,
d07f7b9e 562 int priority,
968f3196 563 pid_t object_pid) {
d025f1e4 564
968f3196 565 char pid[sizeof("_PID=") + DECIMAL_STR_MAX(pid_t)],
ae018d9b
LP
566 uid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)],
567 gid[sizeof("_GID=") + DECIMAL_STR_MAX(gid_t)],
568 owner_uid[sizeof("_SYSTEMD_OWNER_UID=") + DECIMAL_STR_MAX(uid_t)],
d3789917 569 source_time[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)],
968f3196
ZJS
570 o_uid[sizeof("OBJECT_UID=") + DECIMAL_STR_MAX(uid_t)],
571 o_gid[sizeof("OBJECT_GID=") + DECIMAL_STR_MAX(gid_t)],
572 o_owner_uid[sizeof("OBJECT_SYSTEMD_OWNER_UID=") + DECIMAL_STR_MAX(uid_t)];
573 uid_t object_uid;
574 gid_t object_gid;
968f3196 575 char *x;
d025f1e4 576 int r;
ae018d9b 577 char *t, *c;
82499507
LP
578 uid_t realuid = 0, owner = 0, journal_uid;
579 bool owner_valid = false;
ae018d9b 580#ifdef HAVE_AUDIT
968f3196
ZJS
581 char audit_session[sizeof("_AUDIT_SESSION=") + DECIMAL_STR_MAX(uint32_t)],
582 audit_loginuid[sizeof("_AUDIT_LOGINUID=") + DECIMAL_STR_MAX(uid_t)],
583 o_audit_session[sizeof("OBJECT_AUDIT_SESSION=") + DECIMAL_STR_MAX(uint32_t)],
584 o_audit_loginuid[sizeof("OBJECT_AUDIT_LOGINUID=") + DECIMAL_STR_MAX(uid_t)];
ae018d9b
LP
585
586 uint32_t audit;
587 uid_t loginuid;
588#endif
d025f1e4
ZJS
589
590 assert(s);
591 assert(iovec);
592 assert(n > 0);
968f3196 593 assert(n + N_IOVEC_META_FIELDS + (object_pid ? N_IOVEC_OBJECT_FIELDS : 0) <= m);
d025f1e4
ZJS
594
595 if (ucred) {
d025f1e4
ZJS
596 realuid = ucred->uid;
597
de0671ee 598 sprintf(pid, "_PID="PID_FMT, ucred->pid);
c2457105 599 IOVEC_SET_STRING(iovec[n++], pid);
d025f1e4 600
de0671ee 601 sprintf(uid, "_UID="UID_FMT, ucred->uid);
c2457105 602 IOVEC_SET_STRING(iovec[n++], uid);
d025f1e4 603
de0671ee 604 sprintf(gid, "_GID="GID_FMT, ucred->gid);
c2457105 605 IOVEC_SET_STRING(iovec[n++], gid);
d025f1e4
ZJS
606
607 r = get_process_comm(ucred->pid, &t);
608 if (r >= 0) {
63c372cb 609 x = strjoina("_COMM=", t);
d025f1e4 610 free(t);
968f3196 611 IOVEC_SET_STRING(iovec[n++], x);
d025f1e4
ZJS
612 }
613
614 r = get_process_exe(ucred->pid, &t);
615 if (r >= 0) {
63c372cb 616 x = strjoina("_EXE=", t);
d025f1e4 617 free(t);
968f3196 618 IOVEC_SET_STRING(iovec[n++], x);
d025f1e4
ZJS
619 }
620
9bdbc2e2 621 r = get_process_cmdline(ucred->pid, 0, false, &t);
d025f1e4 622 if (r >= 0) {
63c372cb 623 x = strjoina("_CMDLINE=", t);
d025f1e4 624 free(t);
3a832116
SL
625 IOVEC_SET_STRING(iovec[n++], x);
626 }
627
628 r = get_process_capeff(ucred->pid, &t);
629 if (r >= 0) {
63c372cb 630 x = strjoina("_CAP_EFFECTIVE=", t);
3a832116 631 free(t);
968f3196 632 IOVEC_SET_STRING(iovec[n++], x);
d025f1e4
ZJS
633 }
634
0a20e3c1 635#ifdef HAVE_AUDIT
d025f1e4 636 r = audit_session_from_pid(ucred->pid, &audit);
ae018d9b 637 if (r >= 0) {
de0671ee 638 sprintf(audit_session, "_AUDIT_SESSION=%"PRIu32, audit);
ae018d9b
LP
639 IOVEC_SET_STRING(iovec[n++], audit_session);
640 }
d025f1e4
ZJS
641
642 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
7027ff61 643 if (r >= 0) {
de0671ee 644 sprintf(audit_loginuid, "_AUDIT_LOGINUID="UID_FMT, loginuid);
ae018d9b 645 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
d025f1e4 646 }
ae018d9b 647#endif
d025f1e4 648
e9174f29 649 r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &c);
7027ff61 650 if (r >= 0) {
968f3196
ZJS
651 char *session = NULL;
652
63c372cb 653 x = strjoina("_SYSTEMD_CGROUP=", c);
968f3196 654 IOVEC_SET_STRING(iovec[n++], x);
d025f1e4 655
ae018d9b
LP
656 r = cg_path_get_session(c, &t);
657 if (r >= 0) {
63c372cb 658 session = strjoina("_SYSTEMD_SESSION=", t);
ae018d9b 659 free(t);
d025f1e4 660 IOVEC_SET_STRING(iovec[n++], session);
ae018d9b
LP
661 }
662
663 if (cg_path_get_owner_uid(c, &owner) >= 0) {
664 owner_valid = true;
d025f1e4 665
de0671ee 666 sprintf(owner_uid, "_SYSTEMD_OWNER_UID="UID_FMT, owner);
d025f1e4 667 IOVEC_SET_STRING(iovec[n++], owner_uid);
ae018d9b 668 }
d025f1e4 669
ae018d9b 670 if (cg_path_get_unit(c, &t) >= 0) {
63c372cb 671 x = strjoina("_SYSTEMD_UNIT=", t);
ae018d9b 672 free(t);
19cace37
LP
673 IOVEC_SET_STRING(iovec[n++], x);
674 } else if (unit_id && !session) {
63c372cb 675 x = strjoina("_SYSTEMD_UNIT=", unit_id);
19cace37
LP
676 IOVEC_SET_STRING(iovec[n++], x);
677 }
678
679 if (cg_path_get_user_unit(c, &t) >= 0) {
63c372cb 680 x = strjoina("_SYSTEMD_USER_UNIT=", t);
ae018d9b 681 free(t);
968f3196 682 IOVEC_SET_STRING(iovec[n++], x);
19cace37 683 } else if (unit_id && session) {
63c372cb 684 x = strjoina("_SYSTEMD_USER_UNIT=", unit_id);
19cace37
LP
685 IOVEC_SET_STRING(iovec[n++], x);
686 }
ae018d9b 687
0a244b8e 688 if (cg_path_get_slice(c, &t) >= 0) {
63c372cb 689 x = strjoina("_SYSTEMD_SLICE=", t);
0a244b8e
LP
690 free(t);
691 IOVEC_SET_STRING(iovec[n++], x);
692 }
693
ae018d9b 694 free(c);
2d43b190 695 } else if (unit_id) {
63c372cb 696 x = strjoina("_SYSTEMD_UNIT=", unit_id);
2d43b190 697 IOVEC_SET_STRING(iovec[n++], x);
ef1673d1 698 }
d025f1e4 699
d025f1e4 700#ifdef HAVE_SELINUX
6baa7db0 701 if (mac_selinux_use()) {
d682b3a7 702 if (label) {
f8294e41 703 x = alloca(strlen("_SELINUX_CONTEXT=") + label_len + 1);
ae018d9b 704
d682b3a7
LP
705 *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
706 IOVEC_SET_STRING(iovec[n++], x);
707 } else {
708 security_context_t con;
d025f1e4 709
d682b3a7 710 if (getpidcon(ucred->pid, &con) >= 0) {
63c372cb 711 x = strjoina("_SELINUX_CONTEXT=", con);
e7ff4e7f 712
d682b3a7
LP
713 freecon(con);
714 IOVEC_SET_STRING(iovec[n++], x);
715 }
d025f1e4
ZJS
716 }
717 }
718#endif
719 }
968f3196
ZJS
720 assert(n <= m);
721
722 if (object_pid) {
723 r = get_process_uid(object_pid, &object_uid);
724 if (r >= 0) {
de0671ee 725 sprintf(o_uid, "OBJECT_UID="UID_FMT, object_uid);
968f3196
ZJS
726 IOVEC_SET_STRING(iovec[n++], o_uid);
727 }
728
729 r = get_process_gid(object_pid, &object_gid);
730 if (r >= 0) {
de0671ee 731 sprintf(o_gid, "OBJECT_GID="GID_FMT, object_gid);
968f3196
ZJS
732 IOVEC_SET_STRING(iovec[n++], o_gid);
733 }
734
735 r = get_process_comm(object_pid, &t);
736 if (r >= 0) {
63c372cb 737 x = strjoina("OBJECT_COMM=", t);
968f3196
ZJS
738 free(t);
739 IOVEC_SET_STRING(iovec[n++], x);
740 }
741
742 r = get_process_exe(object_pid, &t);
743 if (r >= 0) {
63c372cb 744 x = strjoina("OBJECT_EXE=", t);
968f3196
ZJS
745 free(t);
746 IOVEC_SET_STRING(iovec[n++], x);
747 }
748
749 r = get_process_cmdline(object_pid, 0, false, &t);
750 if (r >= 0) {
63c372cb 751 x = strjoina("OBJECT_CMDLINE=", t);
968f3196
ZJS
752 free(t);
753 IOVEC_SET_STRING(iovec[n++], x);
754 }
755
756#ifdef HAVE_AUDIT
757 r = audit_session_from_pid(object_pid, &audit);
758 if (r >= 0) {
de0671ee 759 sprintf(o_audit_session, "OBJECT_AUDIT_SESSION=%"PRIu32, audit);
968f3196
ZJS
760 IOVEC_SET_STRING(iovec[n++], o_audit_session);
761 }
762
763 r = audit_loginuid_from_pid(object_pid, &loginuid);
764 if (r >= 0) {
de0671ee 765 sprintf(o_audit_loginuid, "OBJECT_AUDIT_LOGINUID="UID_FMT, loginuid);
968f3196
ZJS
766 IOVEC_SET_STRING(iovec[n++], o_audit_loginuid);
767 }
768#endif
769
e9174f29 770 r = cg_pid_get_path_shifted(object_pid, s->cgroup_root, &c);
968f3196 771 if (r >= 0) {
63c372cb 772 x = strjoina("OBJECT_SYSTEMD_CGROUP=", c);
968f3196
ZJS
773 IOVEC_SET_STRING(iovec[n++], x);
774
775 r = cg_path_get_session(c, &t);
776 if (r >= 0) {
63c372cb 777 x = strjoina("OBJECT_SYSTEMD_SESSION=", t);
968f3196
ZJS
778 free(t);
779 IOVEC_SET_STRING(iovec[n++], x);
780 }
781
782 if (cg_path_get_owner_uid(c, &owner) >= 0) {
de0671ee 783 sprintf(o_owner_uid, "OBJECT_SYSTEMD_OWNER_UID="UID_FMT, owner);
968f3196
ZJS
784 IOVEC_SET_STRING(iovec[n++], o_owner_uid);
785 }
786
787 if (cg_path_get_unit(c, &t) >= 0) {
63c372cb 788 x = strjoina("OBJECT_SYSTEMD_UNIT=", t);
968f3196 789 free(t);
19cace37
LP
790 IOVEC_SET_STRING(iovec[n++], x);
791 }
792
793 if (cg_path_get_user_unit(c, &t) >= 0) {
63c372cb 794 x = strjoina("OBJECT_SYSTEMD_USER_UNIT=", t);
968f3196 795 free(t);
968f3196 796 IOVEC_SET_STRING(iovec[n++], x);
19cace37 797 }
968f3196
ZJS
798
799 free(c);
800 }
801 }
802 assert(n <= m);
d025f1e4
ZJS
803
804 if (tv) {
ae018d9b 805 sprintf(source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu", (unsigned long long) timeval_load(tv));
a5693989 806 IOVEC_SET_STRING(iovec[n++], source_time);
d025f1e4
ZJS
807 }
808
809 /* Note that strictly speaking storing the boot id here is
810 * redundant since the entry includes this in-line
811 * anyway. However, we need this indexed, too. */
0c24bb23
LP
812 if (!isempty(s->boot_id_field))
813 IOVEC_SET_STRING(iovec[n++], s->boot_id_field);
d025f1e4 814
0c24bb23
LP
815 if (!isempty(s->machine_id_field))
816 IOVEC_SET_STRING(iovec[n++], s->machine_id_field);
d025f1e4 817
0c24bb23
LP
818 if (!isempty(s->hostname_field))
819 IOVEC_SET_STRING(iovec[n++], s->hostname_field);
d025f1e4
ZJS
820
821 assert(n <= m);
822
da499392 823 if (s->split_mode == SPLIT_UID && realuid > 0)
40adcda8 824 /* Split up strictly by any UID */
759c945a 825 journal_uid = realuid;
82499507 826 else if (s->split_mode == SPLIT_LOGIN && realuid > 0 && owner_valid && owner > 0)
edc3797f
LP
827 /* Split up by login UIDs. We do this only if the
828 * realuid is not root, in order not to accidentally
829 * leak privileged information to the user that is
830 * logged by a privileged process that is part of an
7517e174 831 * unprivileged session. */
8a0889df 832 journal_uid = owner;
da499392
KS
833 else
834 journal_uid = 0;
759c945a 835
d07f7b9e 836 write_to_journal(s, journal_uid, iovec, n, priority);
d025f1e4
ZJS
837}
838
839void server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
840 char mid[11 + 32 + 1];
841 char buffer[16 + LINE_MAX + 1];
b6fa2555 842 struct iovec iovec[N_IOVEC_META_FIELDS + 6];
d025f1e4
ZJS
843 int n = 0;
844 va_list ap;
b92bea5d 845 struct ucred ucred = {};
d025f1e4
ZJS
846
847 assert(s);
848 assert(format);
849
b6fa2555
EV
850 IOVEC_SET_STRING(iovec[n++], "SYSLOG_FACILITY=3");
851 IOVEC_SET_STRING(iovec[n++], "SYSLOG_IDENTIFIER=systemd-journald");
852
d025f1e4
ZJS
853 IOVEC_SET_STRING(iovec[n++], "PRIORITY=6");
854 IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
855
856 memcpy(buffer, "MESSAGE=", 8);
857 va_start(ap, format);
858 vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
859 va_end(ap);
d025f1e4
ZJS
860 IOVEC_SET_STRING(iovec[n++], buffer);
861
862 if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
e2cc6eca 863 snprintf(mid, sizeof(mid), LOG_MESSAGE_ID(message_id));
d025f1e4
ZJS
864 IOVEC_SET_STRING(iovec[n++], mid);
865 }
866
d025f1e4
ZJS
867 ucred.pid = getpid();
868 ucred.uid = getuid();
869 ucred.gid = getgid();
870
d07f7b9e 871 dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, LOG_INFO, 0);
d025f1e4
ZJS
872}
873
874void server_dispatch_message(
875 Server *s,
876 struct iovec *iovec, unsigned n, unsigned m,
3b3154df
LP
877 const struct ucred *ucred,
878 const struct timeval *tv,
d025f1e4
ZJS
879 const char *label, size_t label_len,
880 const char *unit_id,
968f3196
ZJS
881 int priority,
882 pid_t object_pid) {
d025f1e4 883
7027ff61 884 int rl, r;
7fd1b19b 885 _cleanup_free_ char *path = NULL;
8580d1f7 886 uint64_t available = 0;
db91ea32 887 char *c;
d025f1e4
ZJS
888
889 assert(s);
890 assert(iovec || n == 0);
891
892 if (n == 0)
893 return;
894
895 if (LOG_PRI(priority) > s->max_level_store)
896 return;
897
2f5df74a
HHPF
898 /* Stop early in case the information will not be stored
899 * in a journal. */
900 if (s->storage == STORAGE_NONE)
901 return;
902
d025f1e4
ZJS
903 if (!ucred)
904 goto finish;
905
e9174f29 906 r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &path);
7027ff61 907 if (r < 0)
d025f1e4
ZJS
908 goto finish;
909
910 /* example: /user/lennart/3/foobar
911 * /system/dbus.service/foobar
912 *
913 * So let's cut of everything past the third /, since that is
914 * where user directories start */
915
916 c = strchr(path, '/');
917 if (c) {
918 c = strchr(c+1, '/');
919 if (c) {
920 c = strchr(c+1, '/');
921 if (c)
922 *c = 0;
923 }
924 }
925
8580d1f7
LP
926 (void) determine_space(s, false, false, &available, NULL);
927 rl = journal_rate_limit_test(s->rate_limit, path, priority & LOG_PRIMASK, available);
db91ea32 928 if (rl == 0)
d025f1e4 929 return;
d025f1e4
ZJS
930
931 /* Write a suppression message if we suppressed something */
932 if (rl > 1)
db91ea32
ZJS
933 server_driver_message(s, SD_MESSAGE_JOURNAL_DROPPED,
934 "Suppressed %u messages from %s", rl - 1, path);
d025f1e4
ZJS
935
936finish:
d07f7b9e 937 dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, priority, object_pid);
d025f1e4
ZJS
938}
939
940
caa2f4c0 941static int system_journal_open(Server *s, bool flush_requested) {
84267e40 942 const char *fn;
09eba4d4 943 int r = 0;
d025f1e4
ZJS
944
945 if (!s->system_journal &&
946 (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
caa2f4c0
ZJS
947 (flush_requested
948 || access("/run/systemd/journal/flushed", F_OK) >= 0)) {
d025f1e4
ZJS
949
950 /* If in auto mode: first try to create the machine
951 * path, but not the prefix.
952 *
953 * If in persistent mode: create /var/log/journal and
954 * the machine path */
955
956 if (s->storage == STORAGE_PERSISTENT)
ac892057 957 (void) mkdir_p("/var/log/journal/", 0755);
d025f1e4 958
8580d1f7 959 fn = strjoina("/var/log/journal/", SERVER_MACHINE_ID(s));
d025f1e4 960 (void) mkdir(fn, 0755);
d025f1e4 961
63c372cb 962 fn = strjoina(fn, "/system.journal");
d025f1e4 963 r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
8580d1f7 964 if (r >= 0) {
d025f1e4 965 server_fix_perms(s, s->system_journal, 0);
8580d1f7
LP
966 (void) determine_space_for(s, &s->system_metrics, "/var/log/journal/", "System journal", true, true, NULL, NULL);
967 } else if (r < 0) {
433dd100 968 if (r != -ENOENT && r != -EROFS)
da927ba9 969 log_warning_errno(r, "Failed to open system journal: %m");
e40ec7ae 970
433dd100
LN
971 r = 0;
972 }
d025f1e4
ZJS
973 }
974
975 if (!s->runtime_journal &&
976 (s->storage != STORAGE_NONE)) {
977
8580d1f7 978 fn = strjoina("/run/log/journal/", SERVER_MACHINE_ID(s), "/system.journal");
d025f1e4
ZJS
979
980 if (s->system_journal) {
981
982 /* Try to open the runtime journal, but only
983 * if it already exists, so that we can flush
984 * it into the system journal */
985
986 r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
d025f1e4
ZJS
987 if (r < 0) {
988 if (r != -ENOENT)
da927ba9 989 log_warning_errno(r, "Failed to open runtime journal: %m");
d025f1e4
ZJS
990
991 r = 0;
992 }
993
994 } else {
995
996 /* OK, we really need the runtime journal, so create
997 * it if necessary. */
998
fc1d70af
LP
999 (void) mkdir("/run/log", 0755);
1000 (void) mkdir("/run/log/journal", 0755);
1001 (void) mkdir_parents(fn, 0750);
1002
d025f1e4 1003 r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
23bbb0de
MS
1004 if (r < 0)
1005 return log_error_errno(r, "Failed to open runtime journal: %m");
d025f1e4
ZJS
1006 }
1007
8580d1f7 1008 if (s->runtime_journal) {
d025f1e4 1009 server_fix_perms(s, s->runtime_journal, 0);
8580d1f7
LP
1010 (void) determine_space_for(s, &s->runtime_metrics, "/run/log/journal/", "Runtime journal", true, true, NULL, NULL);
1011 }
d025f1e4
ZJS
1012 }
1013
1014 return r;
1015}
1016
1017int server_flush_to_var(Server *s) {
d025f1e4
ZJS
1018 sd_id128_t machine;
1019 sd_journal *j = NULL;
fbb63411
LP
1020 char ts[FORMAT_TIMESPAN_MAX];
1021 usec_t start;
1022 unsigned n = 0;
1023 int r;
d025f1e4
ZJS
1024
1025 assert(s);
1026
1027 if (s->storage != STORAGE_AUTO &&
1028 s->storage != STORAGE_PERSISTENT)
1029 return 0;
1030
1031 if (!s->runtime_journal)
1032 return 0;
1033
8580d1f7 1034 (void) system_journal_open(s, true);
d025f1e4
ZJS
1035
1036 if (!s->system_journal)
1037 return 0;
1038
1039 log_debug("Flushing to /var...");
1040
fbb63411
LP
1041 start = now(CLOCK_MONOTONIC);
1042
d025f1e4 1043 r = sd_id128_get_machine(&machine);
00a16861 1044 if (r < 0)
d025f1e4 1045 return r;
d025f1e4
ZJS
1046
1047 r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
23bbb0de
MS
1048 if (r < 0)
1049 return log_error_errno(r, "Failed to read runtime journal: %m");
d025f1e4 1050
93b73b06
LP
1051 sd_journal_set_data_threshold(j, 0);
1052
d025f1e4
ZJS
1053 SD_JOURNAL_FOREACH(j) {
1054 Object *o = NULL;
1055 JournalFile *f;
1056
1057 f = j->current_file;
1058 assert(f && f->current_offset > 0);
1059
fbb63411
LP
1060 n++;
1061
d025f1e4
ZJS
1062 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1063 if (r < 0) {
da927ba9 1064 log_error_errno(r, "Can't read entry: %m");
d025f1e4
ZJS
1065 goto finish;
1066 }
1067
1068 r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
1069 if (r >= 0)
1070 continue;
1071
1072 if (!shall_try_append_again(s->system_journal, r)) {
da927ba9 1073 log_error_errno(r, "Can't write entry: %m");
d025f1e4
ZJS
1074 goto finish;
1075 }
1076
1077 server_rotate(s);
8580d1f7 1078 server_vacuum(s, false, false);
d025f1e4 1079
253f59df
LP
1080 if (!s->system_journal) {
1081 log_notice("Didn't flush runtime journal since rotation of system journal wasn't successful.");
1082 r = -EIO;
1083 goto finish;
1084 }
1085
d025f1e4
ZJS
1086 log_debug("Retrying write.");
1087 r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
1088 if (r < 0) {
da927ba9 1089 log_error_errno(r, "Can't write entry: %m");
d025f1e4
ZJS
1090 goto finish;
1091 }
1092 }
1093
804ae586
LP
1094 r = 0;
1095
d025f1e4
ZJS
1096finish:
1097 journal_file_post_change(s->system_journal);
1098
804ae586 1099 s->runtime_journal = journal_file_close(s->runtime_journal);
d025f1e4
ZJS
1100
1101 if (r >= 0)
c6878637 1102 (void) rm_rf("/run/log/journal", REMOVE_ROOT);
d025f1e4 1103
763c7aa2 1104 sd_journal_close(j);
d025f1e4 1105
fbb63411
LP
1106 server_driver_message(s, SD_ID128_NULL, "Time spent on flushing to /var is %s for %u entries.", format_timespan(ts, sizeof(ts), now(CLOCK_MONOTONIC) - start, 0), n);
1107
d025f1e4
ZJS
1108 return r;
1109}
1110
8531ae70 1111int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
f9a810be 1112 Server *s = userdata;
a315ac4e
LP
1113 struct ucred *ucred = NULL;
1114 struct timeval *tv = NULL;
1115 struct cmsghdr *cmsg;
1116 char *label = NULL;
1117 size_t label_len = 0, m;
1118 struct iovec iovec;
1119 ssize_t n;
1120 int *fds = NULL, v = 0;
1121 unsigned n_fds = 0;
1122
1123 union {
1124 struct cmsghdr cmsghdr;
1125
1126 /* We use NAME_MAX space for the SELinux label
1127 * here. The kernel currently enforces no
1128 * limit, but according to suggestions from
1129 * the SELinux people this will change and it
1130 * will probably be identical to NAME_MAX. For
1131 * now we use that, but this should be updated
1132 * one day when the final limit is known. */
1133 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
1134 CMSG_SPACE(sizeof(struct timeval)) +
1135 CMSG_SPACE(sizeof(int)) + /* fd */
1136 CMSG_SPACE(NAME_MAX)]; /* selinux label */
1137 } control = {};
1138
1139 union sockaddr_union sa = {};
1140
1141 struct msghdr msghdr = {
1142 .msg_iov = &iovec,
1143 .msg_iovlen = 1,
1144 .msg_control = &control,
1145 .msg_controllen = sizeof(control),
1146 .msg_name = &sa,
1147 .msg_namelen = sizeof(sa),
1148 };
f9a810be 1149
d025f1e4 1150 assert(s);
875c2e22 1151 assert(fd == s->native_fd || fd == s->syslog_fd || fd == s->audit_fd);
f9a810be
LP
1152
1153 if (revents != EPOLLIN) {
1154 log_error("Got invalid event from epoll for datagram fd: %"PRIx32, revents);
1155 return -EIO;
1156 }
1157
a315ac4e
LP
1158 /* Try to get the right size, if we can. (Not all
1159 * sockets support SIOCINQ, hence we just try, but
1160 * don't rely on it. */
1161 (void) ioctl(fd, SIOCINQ, &v);
d025f1e4 1162
a315ac4e
LP
1163 /* Fix it up, if it is too small. We use the same fixed value as auditd here. Awful! */
1164 m = PAGE_ALIGN(MAX3((size_t) v + 1,
1165 (size_t) LINE_MAX,
1166 ALIGN(sizeof(struct nlmsghdr)) + ALIGN((size_t) MAX_AUDIT_MESSAGE_LENGTH)) + 1);
d025f1e4 1167
a315ac4e
LP
1168 if (!GREEDY_REALLOC(s->buffer, s->buffer_size, m))
1169 return log_oom();
875c2e22 1170
a315ac4e
LP
1171 iovec.iov_base = s->buffer;
1172 iovec.iov_len = s->buffer_size - 1; /* Leave room for trailing NUL we add later */
d025f1e4 1173
a315ac4e
LP
1174 n = recvmsg(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
1175 if (n < 0) {
1176 if (errno == EINTR || errno == EAGAIN)
1177 return 0;
875c2e22 1178
a315ac4e
LP
1179 return log_error_errno(errno, "recvmsg() failed: %m");
1180 }
875c2e22 1181
a315ac4e
LP
1182 CMSG_FOREACH(cmsg, &msghdr) {
1183
1184 if (cmsg->cmsg_level == SOL_SOCKET &&
1185 cmsg->cmsg_type == SCM_CREDENTIALS &&
1186 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
1187 ucred = (struct ucred*) CMSG_DATA(cmsg);
1188 else if (cmsg->cmsg_level == SOL_SOCKET &&
1189 cmsg->cmsg_type == SCM_SECURITY) {
1190 label = (char*) CMSG_DATA(cmsg);
1191 label_len = cmsg->cmsg_len - CMSG_LEN(0);
1192 } else if (cmsg->cmsg_level == SOL_SOCKET &&
1193 cmsg->cmsg_type == SO_TIMESTAMP &&
1194 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
1195 tv = (struct timeval*) CMSG_DATA(cmsg);
1196 else if (cmsg->cmsg_level == SOL_SOCKET &&
1197 cmsg->cmsg_type == SCM_RIGHTS) {
1198 fds = (int*) CMSG_DATA(cmsg);
1199 n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
d025f1e4 1200 }
a315ac4e 1201 }
d025f1e4 1202
a315ac4e
LP
1203 /* And a trailing NUL, just in case */
1204 s->buffer[n] = 0;
1205
1206 if (fd == s->syslog_fd) {
1207 if (n > 0 && n_fds == 0)
1208 server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
1209 else if (n_fds > 0)
1210 log_warning("Got file descriptors via syslog socket. Ignoring.");
1211
1212 } else if (fd == s->native_fd) {
1213 if (n > 0 && n_fds == 0)
1214 server_process_native_message(s, s->buffer, n, ucred, tv, label, label_len);
1215 else if (n == 0 && n_fds == 1)
1216 server_process_native_file(s, fds[0], ucred, tv, label, label_len);
1217 else if (n_fds > 0)
1218 log_warning("Got too many file descriptors via native socket. Ignoring.");
1219
1220 } else {
1221 assert(fd == s->audit_fd);
1222
1223 if (n > 0 && n_fds == 0)
1224 server_process_audit_message(s, s->buffer, n, ucred, &sa, msghdr.msg_namelen);
1225 else if (n_fds > 0)
1226 log_warning("Got file descriptors via audit socket. Ignoring.");
f9a810be 1227 }
a315ac4e
LP
1228
1229 close_many(fds, n_fds);
1230 return 0;
f9a810be 1231}
d025f1e4 1232
f9a810be
LP
1233static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
1234 Server *s = userdata;
d025f1e4 1235
f9a810be 1236 assert(s);
d025f1e4 1237
f9a810be 1238 log_info("Received request to flush runtime journal from PID %"PRIu32, si->ssi_pid);
d025f1e4 1239
f9a810be
LP
1240 server_flush_to_var(s);
1241 server_sync(s);
8580d1f7 1242 server_vacuum(s, false, false);
d025f1e4 1243
ac5b0c13 1244 (void) touch("/run/systemd/journal/flushed");
74055aa7 1245
f9a810be
LP
1246 return 0;
1247}
d025f1e4 1248
f9a810be
LP
1249static int dispatch_sigusr2(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
1250 Server *s = userdata;
d025f1e4 1251
f9a810be 1252 assert(s);
d025f1e4 1253
f9a810be
LP
1254 log_info("Received request to rotate journal from PID %"PRIu32, si->ssi_pid);
1255 server_rotate(s);
8580d1f7 1256 server_vacuum(s, true, true);
d025f1e4 1257
f9a810be
LP
1258 return 0;
1259}
d025f1e4 1260
f9a810be
LP
1261static int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
1262 Server *s = userdata;
d025f1e4 1263
f9a810be 1264 assert(s);
d025f1e4 1265
4daf54a8 1266 log_received_signal(LOG_INFO, si);
d025f1e4 1267
6203e07a 1268 sd_event_exit(s->event, 0);
d025f1e4
ZJS
1269 return 0;
1270}
1271
f9a810be 1272static int setup_signals(Server *s) {
f9a810be 1273 int r;
d025f1e4
ZJS
1274
1275 assert(s);
1276
72c0a2c2 1277 assert(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -1) >= 0);
d025f1e4 1278
151b9b96 1279 r = sd_event_add_signal(s->event, &s->sigusr1_event_source, SIGUSR1, dispatch_sigusr1, s);
f9a810be
LP
1280 if (r < 0)
1281 return r;
1282
151b9b96 1283 r = sd_event_add_signal(s->event, &s->sigusr2_event_source, SIGUSR2, dispatch_sigusr2, s);
f9a810be
LP
1284 if (r < 0)
1285 return r;
d025f1e4 1286
151b9b96 1287 r = sd_event_add_signal(s->event, &s->sigterm_event_source, SIGTERM, dispatch_sigterm, s);
f9a810be
LP
1288 if (r < 0)
1289 return r;
d025f1e4 1290
151b9b96 1291 r = sd_event_add_signal(s->event, &s->sigint_event_source, SIGINT, dispatch_sigterm, s);
f9a810be
LP
1292 if (r < 0)
1293 return r;
d025f1e4
ZJS
1294
1295 return 0;
1296}
1297
1298static int server_parse_proc_cmdline(Server *s) {
7fd1b19b 1299 _cleanup_free_ char *line = NULL;
d581d9d9 1300 const char *p;
74df0fca 1301 int r;
d025f1e4 1302
74df0fca 1303 r = proc_cmdline(&line);
b5884878 1304 if (r < 0) {
da927ba9 1305 log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
d025f1e4 1306 return 0;
b5884878 1307 }
d025f1e4 1308
d581d9d9
SS
1309 p = line;
1310 for(;;) {
7fd1b19b 1311 _cleanup_free_ char *word;
d025f1e4 1312
d581d9d9
SS
1313 r = extract_first_word(&p, &word, NULL, 0);
1314 if (r < 0)
1315 return log_error_errno(r, "Failed to parse journald syntax \"%s\": %m", line);
1316
1317 if (r == 0)
1318 break;
d025f1e4
ZJS
1319
1320 if (startswith(word, "systemd.journald.forward_to_syslog=")) {
1321 r = parse_boolean(word + 35);
1322 if (r < 0)
1323 log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
1324 else
1325 s->forward_to_syslog = r;
1326 } else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
1327 r = parse_boolean(word + 33);
1328 if (r < 0)
1329 log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
1330 else
1331 s->forward_to_kmsg = r;
1332 } else if (startswith(word, "systemd.journald.forward_to_console=")) {
1333 r = parse_boolean(word + 36);
1334 if (r < 0)
1335 log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
1336 else
1337 s->forward_to_console = r;
40b71e89
ST
1338 } else if (startswith(word, "systemd.journald.forward_to_wall=")) {
1339 r = parse_boolean(word + 33);
1340 if (r < 0)
1341 log_warning("Failed to parse forward to wall switch %s. Ignoring.", word + 33);
1342 else
1343 s->forward_to_wall = r;
d025f1e4
ZJS
1344 } else if (startswith(word, "systemd.journald"))
1345 log_warning("Invalid systemd.journald parameter. Ignoring.");
d025f1e4
ZJS
1346 }
1347
804ae586 1348 /* do not warn about state here, since probably systemd already did */
db91ea32 1349 return 0;
d025f1e4
ZJS
1350}
1351
1352static int server_parse_config_file(Server *s) {
d025f1e4
ZJS
1353 assert(s);
1354
a9edaeff
JT
1355 return config_parse_many("/etc/systemd/journald.conf",
1356 CONF_DIRS_NULSTR("systemd/journald.conf"),
1357 "Journal\0",
1358 config_item_perf_lookup, journald_gperf_lookup,
1359 false, s);
d025f1e4
ZJS
1360}
1361
f9a810be
LP
1362static int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) {
1363 Server *s = userdata;
26687bf8
OS
1364
1365 assert(s);
1366
f9a810be 1367 server_sync(s);
26687bf8
OS
1368 return 0;
1369}
1370
d07f7b9e 1371int server_schedule_sync(Server *s, int priority) {
26687bf8
OS
1372 int r;
1373
26687bf8
OS
1374 assert(s);
1375
d07f7b9e
LP
1376 if (priority <= LOG_CRIT) {
1377 /* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */
1378 server_sync(s);
1379 return 0;
1380 }
1381
26687bf8
OS
1382 if (s->sync_scheduled)
1383 return 0;
1384
f9a810be
LP
1385 if (s->sync_interval_usec > 0) {
1386 usec_t when;
ca267016 1387
6a0f1f6d 1388 r = sd_event_now(s->event, CLOCK_MONOTONIC, &when);
f9a810be
LP
1389 if (r < 0)
1390 return r;
26687bf8 1391
f9a810be
LP
1392 when += s->sync_interval_usec;
1393
1394 if (!s->sync_event_source) {
6a0f1f6d
LP
1395 r = sd_event_add_time(
1396 s->event,
1397 &s->sync_event_source,
1398 CLOCK_MONOTONIC,
1399 when, 0,
1400 server_dispatch_sync, s);
f9a810be
LP
1401 if (r < 0)
1402 return r;
1403
1404 r = sd_event_source_set_priority(s->sync_event_source, SD_EVENT_PRIORITY_IMPORTANT);
1405 } else {
1406 r = sd_event_source_set_time(s->sync_event_source, when);
1407 if (r < 0)
1408 return r;
1409
1410 r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_ONESHOT);
1411 }
26687bf8 1412 if (r < 0)
f9a810be 1413 return r;
26687bf8 1414
f9a810be
LP
1415 s->sync_scheduled = true;
1416 }
26687bf8
OS
1417
1418 return 0;
1419}
1420
0c24bb23
LP
1421static int dispatch_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
1422 Server *s = userdata;
1423
1424 assert(s);
1425
1426 server_cache_hostname(s);
1427 return 0;
1428}
1429
1430static int server_open_hostname(Server *s) {
1431 int r;
1432
1433 assert(s);
1434
1435 s->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
4a62c710
MS
1436 if (s->hostname_fd < 0)
1437 return log_error_errno(errno, "Failed to open /proc/sys/kernel/hostname: %m");
0c24bb23 1438
151b9b96 1439 r = sd_event_add_io(s->event, &s->hostname_event_source, s->hostname_fd, 0, dispatch_hostname_change, s);
0c24bb23 1440 if (r < 0) {
28def94c
DR
1441 /* kernels prior to 3.2 don't support polling this file. Ignore
1442 * the failure. */
1443 if (r == -EPERM) {
e53fc357 1444 log_warning_errno(r, "Failed to register hostname fd in event loop, ignoring: %m");
03e334a1 1445 s->hostname_fd = safe_close(s->hostname_fd);
28def94c
DR
1446 return 0;
1447 }
1448
23bbb0de 1449 return log_error_errno(r, "Failed to register hostname fd in event loop: %m");
0c24bb23
LP
1450 }
1451
1452 r = sd_event_source_set_priority(s->hostname_event_source, SD_EVENT_PRIORITY_IMPORTANT-10);
23bbb0de
MS
1453 if (r < 0)
1454 return log_error_errno(r, "Failed to adjust priority of host name event source: %m");
0c24bb23
LP
1455
1456 return 0;
1457}
1458
d025f1e4 1459int server_init(Server *s) {
13790add 1460 _cleanup_fdset_free_ FDSet *fds = NULL;
d025f1e4 1461 int n, r, fd;
7d18d348 1462 bool no_sockets;
d025f1e4
ZJS
1463
1464 assert(s);
1465
1466 zero(*s);
875c2e22 1467 s->syslog_fd = s->native_fd = s->stdout_fd = s->dev_kmsg_fd = s->audit_fd = s->hostname_fd = -1;
d025f1e4
ZJS
1468 s->compress = true;
1469 s->seal = true;
1470
26687bf8
OS
1471 s->sync_interval_usec = DEFAULT_SYNC_INTERVAL_USEC;
1472 s->sync_scheduled = false;
1473
d025f1e4
ZJS
1474 s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
1475 s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
1476
40b71e89 1477 s->forward_to_wall = true;
d025f1e4 1478
e150e820
MB
1479 s->max_file_usec = DEFAULT_MAX_FILE_USEC;
1480
d025f1e4
ZJS
1481 s->max_level_store = LOG_DEBUG;
1482 s->max_level_syslog = LOG_DEBUG;
1483 s->max_level_kmsg = LOG_NOTICE;
1484 s->max_level_console = LOG_INFO;
40b71e89 1485 s->max_level_wall = LOG_EMERG;
d025f1e4 1486
8580d1f7
LP
1487 journal_reset_metrics(&s->system_metrics);
1488 journal_reset_metrics(&s->runtime_metrics);
d025f1e4
ZJS
1489
1490 server_parse_config_file(s);
1491 server_parse_proc_cmdline(s);
8580d1f7 1492
d288f79f 1493 if (!!s->rate_limit_interval ^ !!s->rate_limit_burst) {
b1389b0d
ZJS
1494 log_debug("Setting both rate limit interval and burst from "USEC_FMT",%u to 0,0",
1495 s->rate_limit_interval, s->rate_limit_burst);
d288f79f
ZJS
1496 s->rate_limit_interval = s->rate_limit_burst = 0;
1497 }
d025f1e4 1498
8580d1f7 1499 (void) mkdir_p("/run/systemd/journal", 0755);
d025f1e4 1500
43cf8388 1501 s->user_journals = ordered_hashmap_new(NULL);
d025f1e4
ZJS
1502 if (!s->user_journals)
1503 return log_oom();
1504
1505 s->mmap = mmap_cache_new();
1506 if (!s->mmap)
1507 return log_oom();
1508
f9a810be 1509 r = sd_event_default(&s->event);
23bbb0de
MS
1510 if (r < 0)
1511 return log_error_errno(r, "Failed to create event loop: %m");
d025f1e4 1512
f9a810be
LP
1513 sd_event_set_watchdog(s->event, true);
1514
d025f1e4 1515 n = sd_listen_fds(true);
23bbb0de
MS
1516 if (n < 0)
1517 return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
d025f1e4
ZJS
1518
1519 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
1520
1521 if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
1522
1523 if (s->native_fd >= 0) {
1524 log_error("Too many native sockets passed.");
1525 return -EINVAL;
1526 }
1527
1528 s->native_fd = fd;
1529
1530 } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
1531
1532 if (s->stdout_fd >= 0) {
1533 log_error("Too many stdout sockets passed.");
1534 return -EINVAL;
1535 }
1536
1537 s->stdout_fd = fd;
1538
03ee5c38
LP
1539 } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0 ||
1540 sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/dev-log", 0) > 0) {
d025f1e4
ZJS
1541
1542 if (s->syslog_fd >= 0) {
1543 log_error("Too many /dev/log sockets passed.");
1544 return -EINVAL;
1545 }
1546
1547 s->syslog_fd = fd;
1548
875c2e22
LP
1549 } else if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
1550
1551 if (s->audit_fd >= 0) {
1552 log_error("Too many audit sockets passed.");
1553 return -EINVAL;
1554 }
1555
1556 s->audit_fd = fd;
1557
4ec3cd73 1558 } else {
4ec3cd73 1559
13790add
LP
1560 if (!fds) {
1561 fds = fdset_new();
1562 if (!fds)
1563 return log_oom();
1564 }
4ec3cd73 1565
13790add
LP
1566 r = fdset_put(fds, fd);
1567 if (r < 0)
1568 return log_oom();
4ec3cd73 1569 }
d025f1e4
ZJS
1570 }
1571
15d91bff
ZJS
1572 /* Try to restore streams, but don't bother if this fails */
1573 (void) server_restore_streams(s, fds);
d025f1e4 1574
13790add
LP
1575 if (fdset_size(fds) > 0) {
1576 log_warning("%u unknown file descriptors passed, closing.", fdset_size(fds));
1577 fds = fdset_free(fds);
1578 }
1579
7d18d348
ZJS
1580 no_sockets = s->native_fd < 0 && s->stdout_fd < 0 && s->syslog_fd < 0 && s->audit_fd < 0;
1581
1582 /* always open stdout, syslog, native, and kmsg sockets */
37b7affe
ZJS
1583
1584 /* systemd-journald.socket: /run/systemd/journal/stdout */
15d91bff
ZJS
1585 r = server_open_stdout_socket(s);
1586 if (r < 0)
1587 return r;
1588
37b7affe 1589 /* systemd-journald-dev-log.socket: /run/systemd/journal/dev-log */
13790add 1590 r = server_open_syslog_socket(s);
d025f1e4
ZJS
1591 if (r < 0)
1592 return r;
1593
37b7affe 1594 /* systemd-journald.socket: /run/systemd/journal/socket */
13790add 1595 r = server_open_native_socket(s);
d025f1e4
ZJS
1596 if (r < 0)
1597 return r;
1598
37b7affe 1599 /* /dev/ksmg */
d025f1e4
ZJS
1600 r = server_open_dev_kmsg(s);
1601 if (r < 0)
1602 return r;
1603
7d18d348
ZJS
1604 /* Unless we got *some* sockets and not audit, open audit socket */
1605 if (s->audit_fd >= 0 || no_sockets) {
1606 r = server_open_audit(s);
1607 if (r < 0)
1608 return r;
1609 }
875c2e22 1610
d025f1e4
ZJS
1611 r = server_open_kernel_seqnum(s);
1612 if (r < 0)
1613 return r;
1614
0c24bb23
LP
1615 r = server_open_hostname(s);
1616 if (r < 0)
1617 return r;
1618
f9a810be 1619 r = setup_signals(s);
d025f1e4
ZJS
1620 if (r < 0)
1621 return r;
1622
1623 s->udev = udev_new();
1624 if (!s->udev)
1625 return -ENOMEM;
1626
f9a810be 1627 s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
d025f1e4
ZJS
1628 if (!s->rate_limit)
1629 return -ENOMEM;
1630
e9174f29
LP
1631 r = cg_get_root_path(&s->cgroup_root);
1632 if (r < 0)
1633 return r;
1634
0c24bb23
LP
1635 server_cache_hostname(s);
1636 server_cache_boot_id(s);
1637 server_cache_machine_id(s);
1638
804ae586 1639 return system_journal_open(s, false);
d025f1e4
ZJS
1640}
1641
1642void server_maybe_append_tags(Server *s) {
1643#ifdef HAVE_GCRYPT
1644 JournalFile *f;
1645 Iterator i;
1646 usec_t n;
1647
1648 n = now(CLOCK_REALTIME);
1649
1650 if (s->system_journal)
1651 journal_file_maybe_append_tag(s->system_journal, n);
1652
43cf8388 1653 ORDERED_HASHMAP_FOREACH(f, s->user_journals, i)
d025f1e4
ZJS
1654 journal_file_maybe_append_tag(f, n);
1655#endif
1656}
1657
1658void server_done(Server *s) {
1659 JournalFile *f;
1660 assert(s);
1661
1662 while (s->stdout_streams)
1663 stdout_stream_free(s->stdout_streams);
1664
1665 if (s->system_journal)
1666 journal_file_close(s->system_journal);
1667
1668 if (s->runtime_journal)
1669 journal_file_close(s->runtime_journal);
1670
43cf8388 1671 while ((f = ordered_hashmap_steal_first(s->user_journals)))
d025f1e4
ZJS
1672 journal_file_close(f);
1673
43cf8388 1674 ordered_hashmap_free(s->user_journals);
d025f1e4 1675
f9a810be
LP
1676 sd_event_source_unref(s->syslog_event_source);
1677 sd_event_source_unref(s->native_event_source);
1678 sd_event_source_unref(s->stdout_event_source);
1679 sd_event_source_unref(s->dev_kmsg_event_source);
875c2e22 1680 sd_event_source_unref(s->audit_event_source);
f9a810be
LP
1681 sd_event_source_unref(s->sync_event_source);
1682 sd_event_source_unref(s->sigusr1_event_source);
1683 sd_event_source_unref(s->sigusr2_event_source);
1684 sd_event_source_unref(s->sigterm_event_source);
1685 sd_event_source_unref(s->sigint_event_source);
0c24bb23 1686 sd_event_source_unref(s->hostname_event_source);
f9a810be 1687 sd_event_unref(s->event);
d025f1e4 1688
03e334a1
LP
1689 safe_close(s->syslog_fd);
1690 safe_close(s->native_fd);
1691 safe_close(s->stdout_fd);
1692 safe_close(s->dev_kmsg_fd);
875c2e22 1693 safe_close(s->audit_fd);
03e334a1 1694 safe_close(s->hostname_fd);
0c24bb23 1695
d025f1e4
ZJS
1696 if (s->rate_limit)
1697 journal_rate_limit_free(s->rate_limit);
1698
1699 if (s->kernel_seqnum)
1700 munmap(s->kernel_seqnum, sizeof(uint64_t));
1701
1702 free(s->buffer);
1703 free(s->tty_path);
e9174f29 1704 free(s->cgroup_root);
99d0966e 1705 free(s->hostname_field);
d025f1e4
ZJS
1706
1707 if (s->mmap)
1708 mmap_cache_unref(s->mmap);
1709
3e044c49 1710 udev_unref(s->udev);
d025f1e4 1711}
8580d1f7
LP
1712
1713static const char* const storage_table[_STORAGE_MAX] = {
1714 [STORAGE_AUTO] = "auto",
1715 [STORAGE_VOLATILE] = "volatile",
1716 [STORAGE_PERSISTENT] = "persistent",
1717 [STORAGE_NONE] = "none"
1718};
1719
1720DEFINE_STRING_TABLE_LOOKUP(storage, Storage);
1721DEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
1722
1723static const char* const split_mode_table[_SPLIT_MAX] = {
1724 [SPLIT_LOGIN] = "login",
1725 [SPLIT_UID] = "uid",
1726 [SPLIT_NONE] = "none",
1727};
1728
1729DEFINE_STRING_TABLE_LOOKUP(split_mode, SplitMode);
1730DEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode, split_mode, SplitMode, "Failed to parse split mode setting");