1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "coredump-util.h"
4 #include "extract-word.h"
5 #include "fileio.h"
6 #include "string-table.h"
7
8 static const char *const coredump_filter_table[_COREDUMP_FILTER_MAX] = {
9 [COREDUMP_FILTER_PRIVATE_ANONYMOUS] = "private-anonymous",
10 [COREDUMP_FILTER_SHARED_ANONYMOUS] = "shared-anonymous",
11 [COREDUMP_FILTER_PRIVATE_FILE_BACKED] = "private-file-backed",
12 [COREDUMP_FILTER_SHARED_FILE_BACKED] = "shared-file-backed",
13 [COREDUMP_FILTER_ELF_HEADERS] = "elf-headers",
14 [COREDUMP_FILTER_PRIVATE_HUGE] = "private-huge",
15 [COREDUMP_FILTER_SHARED_HUGE] = "shared-huge",
16 [COREDUMP_FILTER_PRIVATE_DAX] = "private-dax",
17 [COREDUMP_FILTER_SHARED_DAX] = "shared-dax",
18 };
19
20 DEFINE_STRING_TABLE_LOOKUP(coredump_filter, CoredumpFilter);
21
coredump_filter_mask_from_string(const char * s,uint64_t * ret)22 int coredump_filter_mask_from_string(const char *s, uint64_t *ret) {
23 uint64_t m = 0;
24
25 assert(s);
26 assert(ret);
27
28 for (;;) {
29 _cleanup_free_ char *n = NULL;
30 CoredumpFilter v;
31 int r;
32
33 r = extract_first_word(&s, &n, NULL, 0);
34 if (r < 0)
35 return r;
36 if (r == 0)
37 break;
38
39 if (streq(n, "default")) {
40 m |= COREDUMP_FILTER_MASK_DEFAULT;
41 continue;
42 }
43
44 if (streq(n, "all")) {
45 m = UINT64_MAX;
46 continue;
47 }
48
49 v = coredump_filter_from_string(n);
50 if (v >= 0) {
51 m |= 1u << v;
52 continue;
53 }
54
55 uint64_t x;
56 r = safe_atoux64(n, &x);
57 if (r < 0)
58 return r;
59
60 m |= x;
61 }
62
63 *ret = m;
64 return 0;
65 }
66
set_coredump_filter(uint64_t value)67 int set_coredump_filter(uint64_t value) {
68 char t[STRLEN("0xFFFFFFFF")];
69
70 sprintf(t, "0x%"PRIx64, value);
71
72 return write_string_file("/proc/self/coredump_filter", t,
73 WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);
74 }
75