File: | build-scan/../src/coredump/coredump.c |
Warning: | line 614, column 32 The right operand of '!=' is a garbage value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |||
2 | ||||
3 | #include <errno(*__errno_location ()).h> | |||
4 | #include <stdio.h> | |||
5 | #include <stdio_ext.h> | |||
6 | #include <sys/prctl.h> | |||
7 | #include <sys/xattr.h> | |||
8 | #include <unistd.h> | |||
9 | ||||
10 | #if HAVE_ELFUTILS1 | |||
11 | #include <dwarf.h> | |||
12 | #include <elfutils/libdwfl.h> | |||
13 | #endif | |||
14 | ||||
15 | #include "sd-daemon.h" | |||
16 | #include "sd-journal.h" | |||
17 | #include "sd-login.h" | |||
18 | #include "sd-messages.h" | |||
19 | ||||
20 | #include "acl-util.h" | |||
21 | #include "alloc-util.h" | |||
22 | #include "capability-util.h" | |||
23 | #include "cgroup-util.h" | |||
24 | #include "compress.h" | |||
25 | #include "conf-parser.h" | |||
26 | #include "copy.h" | |||
27 | #include "coredump-vacuum.h" | |||
28 | #include "dirent-util.h" | |||
29 | #include "escape.h" | |||
30 | #include "fd-util.h" | |||
31 | #include "fileio.h" | |||
32 | #include "fs-util.h" | |||
33 | #include "io-util.h" | |||
34 | #include "journal-importer.h" | |||
35 | #include "log.h" | |||
36 | #include "macro.h" | |||
37 | #include "missing.h" | |||
38 | #include "mkdir.h" | |||
39 | #include "parse-util.h" | |||
40 | #include "process-util.h" | |||
41 | #include "signal-util.h" | |||
42 | #include "socket-util.h" | |||
43 | #include "special.h" | |||
44 | #include "stacktrace.h" | |||
45 | #include "string-table.h" | |||
46 | #include "string-util.h" | |||
47 | #include "strv.h" | |||
48 | #include "user-util.h" | |||
49 | #include "util.h" | |||
50 | ||||
51 | /* The maximum size up to which we process coredumps */ | |||
52 | #define PROCESS_SIZE_MAX((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)) ((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)) | |||
53 | ||||
54 | /* The maximum size up to which we leave the coredump around on disk */ | |||
55 | #define EXTERNAL_SIZE_MAX((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)) PROCESS_SIZE_MAX((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)) | |||
56 | ||||
57 | /* The maximum size up to which we store the coredump in the journal */ | |||
58 | #define JOURNAL_SIZE_MAX((size_t) (767LU*1024LU*1024LU)) ((size_t) (767LU*1024LU*1024LU)) | |||
59 | ||||
60 | /* Make sure to not make this larger than the maximum journal entry | |||
61 | * size. See DATA_SIZE_MAX in journald-native.c. */ | |||
62 | assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX)GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_32 { char x[(((size_t) (767LU*1024LU *1024LU)) <= (1024*1024*768u)) ? 0 : -1]; }; GCC diagnostic pop ; | |||
63 | ||||
64 | enum { | |||
65 | /* We use this as array indexes for a couple of special fields we use for | |||
66 | * naming coredump files, and attaching xattrs, and for indexing argv[]. | |||
67 | ||||
68 | * Our pattern for man:systectl(1) kernel.core_pattern is such that the | |||
69 | * kernel passes fields until CONTEXT_RLIMIT as arguments in argv[]. After | |||
70 | * that it gets complicated: the kernel passes "comm" as one or more fields | |||
71 | * starting at index CONTEXT_COMM (in other words, full "comm" is under index | |||
72 | * CONTEXT_COMM when it does not contain spaces, which is the common | |||
73 | * case). This mapping is not reversible, so we prefer to retrieve "comm" | |||
74 | * from /proc. We only fall back to argv[CONTEXT_COMM...] when that fails. | |||
75 | * | |||
76 | * In the internal context[] array, fields before CONTEXT_COMM are the | |||
77 | * strings from argv[], so they should not be freed. The strings at indices | |||
78 | * CONTEXT_COMM and higher are allocated by us and should be freed at the | |||
79 | * end. | |||
80 | */ | |||
81 | CONTEXT_PID, | |||
82 | CONTEXT_UID, | |||
83 | CONTEXT_GID, | |||
84 | CONTEXT_SIGNAL, | |||
85 | CONTEXT_TIMESTAMP, | |||
86 | CONTEXT_RLIMIT, | |||
87 | CONTEXT_HOSTNAME, | |||
88 | CONTEXT_COMM, | |||
89 | CONTEXT_EXE, | |||
90 | CONTEXT_UNIT, | |||
91 | _CONTEXT_MAX | |||
92 | }; | |||
93 | ||||
94 | typedef enum CoredumpStorage { | |||
95 | COREDUMP_STORAGE_NONE, | |||
96 | COREDUMP_STORAGE_EXTERNAL, | |||
97 | COREDUMP_STORAGE_JOURNAL, | |||
98 | _COREDUMP_STORAGE_MAX, | |||
99 | _COREDUMP_STORAGE_INVALID = -1 | |||
100 | } CoredumpStorage; | |||
101 | ||||
102 | static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = { | |||
103 | [COREDUMP_STORAGE_NONE] = "none", | |||
104 | [COREDUMP_STORAGE_EXTERNAL] = "external", | |||
105 | [COREDUMP_STORAGE_JOURNAL] = "journal", | |||
106 | }; | |||
107 | ||||
108 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage)static const char *coredump_storage_to_string(CoredumpStorage i) { if (i < 0 || i >= (CoredumpStorage) __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (coredump_storage_table), typeof(&*(coredump_storage_table ))), sizeof(coredump_storage_table)/sizeof((coredump_storage_table )[0]), ((void)0)))) return ((void*)0); return coredump_storage_table [i]; } static CoredumpStorage coredump_storage_from_string(const char *s) { return (CoredumpStorage) string_table_lookup(coredump_storage_table , __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(coredump_storage_table), typeof(&*(coredump_storage_table ))), sizeof(coredump_storage_table)/sizeof((coredump_storage_table )[0]), ((void)0))), s); }; | |||
109 | static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting")int config_parse_coredump_storage(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line , const char *lvalue, int ltype, const char *rvalue, void *data , void *userdata) { CoredumpStorage *i = data, x; do { if ((__builtin_expect (!!(!(filename)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("filename"), "../src/coredump/coredump.c", 109, __PRETTY_FUNCTION__ ); } while (0); do { if ((__builtin_expect(!!(!(lvalue)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("lvalue"), "../src/coredump/coredump.c" , 109, __PRETTY_FUNCTION__); } while (0); do { if ((__builtin_expect (!!(!(rvalue)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("rvalue"), "../src/coredump/coredump.c", 109, __PRETTY_FUNCTION__ ); } while (0); do { if ((__builtin_expect(!!(!(data)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("data"), "../src/coredump/coredump.c", 109 , __PRETTY_FUNCTION__); } while (0); x = coredump_storage_from_string (rvalue); if (x < 0) { ({ int _level = (3), _e = (0); (log_get_max_level_realm (LOG_REALM_SYSTEMD) >= ((_level) & 0x07)) ? log_syntax_internal (unit, _level, filename, line, _e, "../src/coredump/coredump.c" , 109, __func__, "Failed to parse storage setting" ", ignoring: %s" , rvalue) : -abs(_e); }); return 0; } *i = x; return 0; }; | |||
110 | ||||
111 | static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL; | |||
112 | static bool_Bool arg_compress = true1; | |||
113 | static uint64_t arg_process_size_max = PROCESS_SIZE_MAX((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)); | |||
114 | static uint64_t arg_external_size_max = EXTERNAL_SIZE_MAX((uint64_t) (2LLU*1024LLU*1024LLU*1024LLU)); | |||
115 | static uint64_t arg_journal_size_max = JOURNAL_SIZE_MAX((size_t) (767LU*1024LU*1024LU)); | |||
116 | static uint64_t arg_keep_free = (uint64_t) -1; | |||
117 | static uint64_t arg_max_use = (uint64_t) -1; | |||
118 | ||||
119 | static int parse_config(void) { | |||
120 | static const ConfigTableItem items[] = { | |||
121 | { "Coredump", "Storage", config_parse_coredump_storage, 0, &arg_storage }, | |||
122 | { "Coredump", "Compress", config_parse_bool, 0, &arg_compress }, | |||
123 | { "Coredump", "ProcessSizeMax", config_parse_iec_uint64, 0, &arg_process_size_max }, | |||
124 | { "Coredump", "ExternalSizeMax", config_parse_iec_uint64, 0, &arg_external_size_max }, | |||
125 | { "Coredump", "JournalSizeMax", config_parse_iec_size, 0, &arg_journal_size_max }, | |||
126 | { "Coredump", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free }, | |||
127 | { "Coredump", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use }, | |||
128 | {} | |||
129 | }; | |||
130 | ||||
131 | return config_parse_many_nulstr(PKGSYSCONFDIR"/etc/systemd" "/coredump.conf", | |||
132 | CONF_PATHS_NULSTR("systemd/coredump.conf.d")"/etc/" "systemd/coredump.conf.d" "\0" "/run/" "systemd/coredump.conf.d" "\0" "/usr/local/lib/" "systemd/coredump.conf.d" "\0" "/usr/lib/" "systemd/coredump.conf.d" "\0", | |||
133 | "Coredump\0", | |||
134 | config_item_table_lookup, items, | |||
135 | CONFIG_PARSE_WARN, NULL((void*)0)); | |||
136 | } | |||
137 | ||||
138 | static inline uint64_t storage_size_max(void) { | |||
139 | if (arg_storage == COREDUMP_STORAGE_EXTERNAL) | |||
140 | return arg_external_size_max; | |||
141 | if (arg_storage == COREDUMP_STORAGE_JOURNAL) | |||
142 | return arg_journal_size_max; | |||
143 | assert(arg_storage == COREDUMP_STORAGE_NONE)do { if ((__builtin_expect(!!(!(arg_storage == COREDUMP_STORAGE_NONE )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("arg_storage == COREDUMP_STORAGE_NONE" ), "../src/coredump/coredump.c", 143, __PRETTY_FUNCTION__); } while (0); | |||
144 | return 0; | |||
145 | } | |||
146 | ||||
147 | static int fix_acl(int fd, uid_t uid) { | |||
148 | ||||
149 | #if HAVE_ACL1 | |||
150 | _cleanup_(acl_freep)__attribute__((cleanup(acl_freep))) acl_t acl = NULL((void*)0); | |||
151 | acl_entry_t entry; | |||
152 | acl_permset_t permset; | |||
153 | int r; | |||
154 | ||||
155 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/coredump/coredump.c" , 155, __PRETTY_FUNCTION__); } while (0); | |||
156 | ||||
157 | if (uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY((uid_t) 65534U)) | |||
158 | return 0; | |||
159 | ||||
160 | /* Make sure normal users can read (but not write or delete) | |||
161 | * their own coredumps */ | |||
162 | ||||
163 | acl = acl_get_fd(fd); | |||
164 | if (!acl) | |||
165 | return log_error_errno(errno, "Failed to get ACL: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 165, __func__ , "Failed to get ACL: %m") : -abs(_e); }); | |||
166 | ||||
167 | if (acl_create_entry(&acl, &entry) < 0 || | |||
168 | acl_set_tag_type(entry, ACL_USER(0x02)) < 0 || | |||
169 | acl_set_qualifier(entry, &uid) < 0) | |||
170 | return log_error_errno(errno, "Failed to patch ACL: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 170, __func__ , "Failed to patch ACL: %m") : -abs(_e); }); | |||
171 | ||||
172 | if (acl_get_permset(entry, &permset) < 0 || | |||
173 | acl_add_perm(permset, ACL_READ(0x04)) < 0) | |||
174 | return log_warning_errno(errno, "Failed to patch ACL: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 174, __func__ , "Failed to patch ACL: %m") : -abs(_e); }); | |||
175 | ||||
176 | r = calc_acl_mask_if_needed(&acl); | |||
177 | if (r < 0) | |||
178 | return log_warning_errno(r, "Failed to patch ACL: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 178, __func__, "Failed to patch ACL: %m" ) : -abs(_e); }); | |||
179 | ||||
180 | if (acl_set_fd(fd, acl) < 0) | |||
181 | return log_error_errno(errno, "Failed to apply ACL: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 181, __func__ , "Failed to apply ACL: %m") : -abs(_e); }); | |||
182 | #endif | |||
183 | ||||
184 | return 0; | |||
185 | } | |||
186 | ||||
187 | static int fix_xattr(int fd, const char *context[_CONTEXT_MAX]) { | |||
188 | ||||
189 | static const char * const xattrs[_CONTEXT_MAX] = { | |||
190 | [CONTEXT_PID] = "user.coredump.pid", | |||
191 | [CONTEXT_UID] = "user.coredump.uid", | |||
192 | [CONTEXT_GID] = "user.coredump.gid", | |||
193 | [CONTEXT_SIGNAL] = "user.coredump.signal", | |||
194 | [CONTEXT_TIMESTAMP] = "user.coredump.timestamp", | |||
195 | [CONTEXT_RLIMIT] = "user.coredump.rlimit", | |||
196 | [CONTEXT_HOSTNAME] = "user.coredump.hostname", | |||
197 | [CONTEXT_COMM] = "user.coredump.comm", | |||
198 | [CONTEXT_EXE] = "user.coredump.exe", | |||
199 | }; | |||
200 | ||||
201 | int r = 0; | |||
202 | unsigned i; | |||
203 | ||||
204 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/coredump/coredump.c" , 204, __PRETTY_FUNCTION__); } while (0); | |||
205 | ||||
206 | /* Attach some metadata to coredumps via extended | |||
207 | * attributes. Just because we can. */ | |||
208 | ||||
209 | for (i = 0; i < _CONTEXT_MAX; i++) { | |||
210 | int k; | |||
211 | ||||
212 | if (isempty(context[i]) || !xattrs[i]) | |||
213 | continue; | |||
214 | ||||
215 | k = fsetxattr(fd, xattrs[i], context[i], strlen(context[i]), XATTR_CREATEXATTR_CREATE); | |||
216 | if (k < 0 && r == 0) | |||
217 | r = -errno(*__errno_location ()); | |||
218 | } | |||
219 | ||||
220 | return r; | |||
221 | } | |||
222 | ||||
223 | #define filename_escape(s)xescape((s), "./ ") xescape((s), "./ ") | |||
224 | ||||
225 | static inline const char *coredump_tmpfile_name(const char *s) { | |||
226 | return s ? s : "(unnamed temporary file)"; | |||
227 | } | |||
228 | ||||
229 | static int fix_permissions( | |||
230 | int fd, | |||
231 | const char *filename, | |||
232 | const char *target, | |||
233 | const char *context[_CONTEXT_MAX], | |||
234 | uid_t uid) { | |||
235 | ||||
236 | int r; | |||
237 | ||||
238 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/coredump/coredump.c" , 238, __PRETTY_FUNCTION__); } while (0); | |||
239 | assert(target)do { if ((__builtin_expect(!!(!(target)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("target"), "../src/coredump/coredump.c", 239, __PRETTY_FUNCTION__); } while (0); | |||
240 | assert(context)do { if ((__builtin_expect(!!(!(context)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context"), "../src/coredump/coredump.c" , 240, __PRETTY_FUNCTION__); } while (0); | |||
241 | ||||
242 | /* Ignore errors on these */ | |||
243 | (void) fchmod(fd, 0640); | |||
244 | (void) fix_acl(fd, uid); | |||
245 | (void) fix_xattr(fd, context); | |||
246 | ||||
247 | if (fsync(fd) < 0) | |||
248 | return log_error_errno(errno, "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename))({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 248, __func__ , "Failed to sync coredump %s: %m", coredump_tmpfile_name(filename )) : -abs(_e); }); | |||
249 | ||||
250 | (void) fsync_directory_of_file(fd); | |||
251 | ||||
252 | r = link_tmpfile(fd, filename, target); | |||
253 | if (r < 0) | |||
254 | return log_error_errno(r, "Failed to move coredump %s into place: %m", target)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 254, __func__, "Failed to move coredump %s into place: %m" , target) : -abs(_e); }); | |||
255 | ||||
256 | return 0; | |||
257 | } | |||
258 | ||||
259 | static int maybe_remove_external_coredump(const char *filename, uint64_t size) { | |||
260 | ||||
261 | /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */ | |||
262 | ||||
263 | if (arg_storage == COREDUMP_STORAGE_EXTERNAL && | |||
264 | size <= arg_external_size_max) | |||
265 | return 0; | |||
266 | ||||
267 | if (!filename) | |||
268 | return 1; | |||
269 | ||||
270 | if (unlink(filename) < 0 && errno(*__errno_location ()) != ENOENT2) | |||
271 | return log_error_errno(errno, "Failed to unlink %s: %m", filename)({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 271, __func__ , "Failed to unlink %s: %m", filename) : -abs(_e); }); | |||
272 | ||||
273 | return 1; | |||
274 | } | |||
275 | ||||
276 | static int make_filename(const char *context[_CONTEXT_MAX], char **ret) { | |||
277 | _cleanup_free___attribute__((cleanup(freep))) char *c = NULL((void*)0), *u = NULL((void*)0), *p = NULL((void*)0), *t = NULL((void*)0); | |||
278 | sd_id128_t boot = {}; | |||
279 | int r; | |||
280 | ||||
281 | assert(context)do { if ((__builtin_expect(!!(!(context)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context"), "../src/coredump/coredump.c" , 281, __PRETTY_FUNCTION__); } while (0); | |||
282 | ||||
283 | c = filename_escape(context[CONTEXT_COMM])xescape((context[CONTEXT_COMM]), "./ "); | |||
284 | if (!c) | |||
285 | return -ENOMEM12; | |||
286 | ||||
287 | u = filename_escape(context[CONTEXT_UID])xescape((context[CONTEXT_UID]), "./ "); | |||
288 | if (!u) | |||
289 | return -ENOMEM12; | |||
290 | ||||
291 | r = sd_id128_get_boot(&boot); | |||
292 | if (r < 0) | |||
293 | return r; | |||
294 | ||||
295 | p = filename_escape(context[CONTEXT_PID])xescape((context[CONTEXT_PID]), "./ "); | |||
296 | if (!p) | |||
297 | return -ENOMEM12; | |||
298 | ||||
299 | t = filename_escape(context[CONTEXT_TIMESTAMP])xescape((context[CONTEXT_TIMESTAMP]), "./ "); | |||
300 | if (!t) | |||
301 | return -ENOMEM12; | |||
302 | ||||
303 | if (asprintf(ret, | |||
304 | "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" ".%s.%s000000", | |||
305 | c, | |||
306 | u, | |||
307 | SD_ID128_FORMAT_VAL(boot)(boot).bytes[0], (boot).bytes[1], (boot).bytes[2], (boot).bytes [3], (boot).bytes[4], (boot).bytes[5], (boot).bytes[6], (boot ).bytes[7], (boot).bytes[8], (boot).bytes[9], (boot).bytes[10 ], (boot).bytes[11], (boot).bytes[12], (boot).bytes[13], (boot ).bytes[14], (boot).bytes[15], | |||
308 | p, | |||
309 | t) < 0) | |||
310 | return -ENOMEM12; | |||
311 | ||||
312 | return 0; | |||
313 | } | |||
314 | ||||
315 | static int save_external_coredump( | |||
316 | const char *context[_CONTEXT_MAX], | |||
317 | int input_fd, | |||
318 | char **ret_filename, | |||
319 | int *ret_node_fd, | |||
320 | int *ret_data_fd, | |||
321 | uint64_t *ret_size, | |||
322 | bool_Bool *ret_truncated) { | |||
323 | ||||
324 | _cleanup_free___attribute__((cleanup(freep))) char *fn = NULL((void*)0), *tmp = NULL((void*)0); | |||
325 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; | |||
326 | uint64_t rlimit, process_limit, max_size; | |||
327 | struct stat st; | |||
328 | uid_t uid; | |||
329 | int r; | |||
330 | ||||
331 | assert(context)do { if ((__builtin_expect(!!(!(context)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context"), "../src/coredump/coredump.c" , 331, __PRETTY_FUNCTION__); } while (0); | |||
332 | assert(ret_filename)do { if ((__builtin_expect(!!(!(ret_filename)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret_filename"), "../src/coredump/coredump.c" , 332, __PRETTY_FUNCTION__); } while (0); | |||
333 | assert(ret_node_fd)do { if ((__builtin_expect(!!(!(ret_node_fd)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret_node_fd"), "../src/coredump/coredump.c" , 333, __PRETTY_FUNCTION__); } while (0); | |||
334 | assert(ret_data_fd)do { if ((__builtin_expect(!!(!(ret_data_fd)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret_data_fd"), "../src/coredump/coredump.c" , 334, __PRETTY_FUNCTION__); } while (0); | |||
335 | assert(ret_size)do { if ((__builtin_expect(!!(!(ret_size)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret_size"), "../src/coredump/coredump.c" , 335, __PRETTY_FUNCTION__); } while (0); | |||
336 | ||||
337 | r = parse_uid(context[CONTEXT_UID], &uid); | |||
338 | if (r < 0) | |||
339 | return log_error_errno(r, "Failed to parse UID: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 339, __func__, "Failed to parse UID: %m" ) : -abs(_e); }); | |||
340 | ||||
341 | r = safe_atou64(context[CONTEXT_RLIMIT], &rlimit); | |||
342 | if (r < 0) | |||
343 | return log_error_errno(r, "Failed to parse resource limit: %s", context[CONTEXT_RLIMIT])({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 343, __func__, "Failed to parse resource limit: %s" , context[CONTEXT_RLIMIT]) : -abs(_e); }); | |||
344 | if (rlimit < page_size()) { | |||
345 | /* Is coredumping disabled? Then don't bother saving/processing the coredump. | |||
346 | * Anything below PAGE_SIZE cannot give a readable coredump (the kernel uses | |||
347 | * ELF_EXEC_PAGESIZE which is not easily accessible, but is usually the same as PAGE_SIZE. */ | |||
348 | log_info("Resource limits disable core dumping for process %s (%s).",({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 349, __func__, "Resource limits disable core dumping for process %s (%s)." , context[CONTEXT_PID], context[CONTEXT_COMM]) : -abs(_e); }) | |||
349 | context[CONTEXT_PID], context[CONTEXT_COMM])({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 349, __func__, "Resource limits disable core dumping for process %s (%s)." , context[CONTEXT_PID], context[CONTEXT_COMM]) : -abs(_e); }); | |||
350 | return -EBADSLT57; | |||
351 | } | |||
352 | ||||
353 | process_limit = MAX(arg_process_size_max, storage_size_max())__extension__ ({ const typeof((arg_process_size_max)) __unique_prefix_A33 = ((arg_process_size_max)); const typeof((storage_size_max() )) __unique_prefix_B34 = ((storage_size_max())); __unique_prefix_A33 > __unique_prefix_B34 ? __unique_prefix_A33 : __unique_prefix_B34 ; }); | |||
354 | if (process_limit == 0) { | |||
355 | log_debug("Limits for coredump processing and storage are both 0, not dumping core.")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 355, __func__, "Limits for coredump processing and storage are both 0, not dumping core." ) : -abs(_e); }); | |||
356 | return -EBADSLT57; | |||
357 | } | |||
358 | ||||
359 | /* Never store more than the process configured, or than we actually shall keep or process */ | |||
360 | max_size = MIN(rlimit, process_limit)__extension__ ({ const typeof((rlimit)) __unique_prefix_A35 = ((rlimit)); const typeof((process_limit)) __unique_prefix_B36 = ((process_limit)); __unique_prefix_A35 < __unique_prefix_B36 ? __unique_prefix_A35 : __unique_prefix_B36; }); | |||
361 | ||||
362 | r = make_filename(context, &fn); | |||
363 | if (r < 0) | |||
364 | return log_error_errno(r, "Failed to determine coredump file name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 364, __func__, "Failed to determine coredump file name: %m" ) : -abs(_e); }); | |||
365 | ||||
366 | mkdir_p_label("/var/lib/systemd/coredump", 0755); | |||
367 | ||||
368 | fd = open_tmpfile_linkable(fn, O_RDWR02|O_CLOEXEC02000000, &tmp); | |||
369 | if (fd < 0) | |||
370 | return log_error_errno(fd, "Failed to create temporary file for coredump %s: %m", fn)({ int _level = ((3)), _e = ((fd)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 370, __func__, "Failed to create temporary file for coredump %s: %m" , fn) : -abs(_e); }); | |||
371 | ||||
372 | r = copy_bytes(input_fd, fd, max_size, 0); | |||
373 | if (r < 0) { | |||
374 | log_error_errno(r, "Cannot store coredump of %s (%s): %m", context[CONTEXT_PID], context[CONTEXT_COMM])({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 374, __func__, "Cannot store coredump of %s (%s): %m" , context[CONTEXT_PID], context[CONTEXT_COMM]) : -abs(_e); }); | |||
375 | goto fail; | |||
376 | } | |||
377 | *ret_truncated = r == 1; | |||
378 | if (*ret_truncated) | |||
379 | log_struct(LOG_INFO,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/coredump/coredump.c", 382, __func__, "MESSAGE=" "Core file was truncated to %zu bytes." , max_size, "SIZE_LIMIT=%zu", max_size, "MESSAGE_ID=" "5a" "ad" "d8" "e9" "54" "dc" "4b" "1a" "8c" "95" "4d" "63" "fd" "9e" "11" "37", ((void*)0)) | |||
380 | LOG_MESSAGE("Core file was truncated to %zu bytes.", max_size),log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/coredump/coredump.c", 382, __func__, "MESSAGE=" "Core file was truncated to %zu bytes." , max_size, "SIZE_LIMIT=%zu", max_size, "MESSAGE_ID=" "5a" "ad" "d8" "e9" "54" "dc" "4b" "1a" "8c" "95" "4d" "63" "fd" "9e" "11" "37", ((void*)0)) | |||
381 | "SIZE_LIMIT=%zu", max_size,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/coredump/coredump.c", 382, __func__, "MESSAGE=" "Core file was truncated to %zu bytes." , max_size, "SIZE_LIMIT=%zu", max_size, "MESSAGE_ID=" "5a" "ad" "d8" "e9" "54" "dc" "4b" "1a" "8c" "95" "4d" "63" "fd" "9e" "11" "37", ((void*)0)) | |||
382 | "MESSAGE_ID=" SD_MESSAGE_TRUNCATED_CORE_STR)log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/coredump/coredump.c", 382, __func__, "MESSAGE=" "Core file was truncated to %zu bytes." , max_size, "SIZE_LIMIT=%zu", max_size, "MESSAGE_ID=" "5a" "ad" "d8" "e9" "54" "dc" "4b" "1a" "8c" "95" "4d" "63" "fd" "9e" "11" "37", ((void*)0)); | |||
383 | ||||
384 | if (fstat(fd, &st) < 0) { | |||
385 | log_error_errno(errno, "Failed to fstat core file %s: %m", coredump_tmpfile_name(tmp))({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 385, __func__ , "Failed to fstat core file %s: %m", coredump_tmpfile_name(tmp )) : -abs(_e); }); | |||
386 | goto fail; | |||
387 | } | |||
388 | ||||
389 | if (lseek(fd, 0, SEEK_SET0) == (off_t) -1) { | |||
390 | log_error_errno(errno, "Failed to seek on %s: %m", coredump_tmpfile_name(tmp))({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 390, __func__ , "Failed to seek on %s: %m", coredump_tmpfile_name(tmp)) : - abs(_e); }); | |||
391 | goto fail; | |||
392 | } | |||
393 | ||||
394 | #if HAVE_XZ1 || HAVE_LZ41 | |||
395 | /* If we will remove the coredump anyway, do not compress. */ | |||
396 | if (arg_compress && !maybe_remove_external_coredump(NULL((void*)0), st.st_size)) { | |||
397 | ||||
398 | _cleanup_free___attribute__((cleanup(freep))) char *fn_compressed = NULL((void*)0), *tmp_compressed = NULL((void*)0); | |||
399 | _cleanup_close___attribute__((cleanup(closep))) int fd_compressed = -1; | |||
400 | ||||
401 | fn_compressed = strappend(fn, COMPRESSED_EXT".lz4"); | |||
402 | if (!fn_compressed) { | |||
403 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 403, __func__); | |||
404 | goto uncompressed; | |||
405 | } | |||
406 | ||||
407 | fd_compressed = open_tmpfile_linkable(fn_compressed, O_RDWR02|O_CLOEXEC02000000, &tmp_compressed); | |||
408 | if (fd_compressed < 0) { | |||
409 | log_error_errno(fd_compressed, "Failed to create temporary file for coredump %s: %m", fn_compressed)({ int _level = ((3)), _e = ((fd_compressed)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 409, __func__, "Failed to create temporary file for coredump %s: %m" , fn_compressed) : -abs(_e); }); | |||
410 | goto uncompressed; | |||
411 | } | |||
412 | ||||
413 | r = compress_streamcompress_stream_lz4(fd, fd_compressed, -1); | |||
414 | if (r < 0) { | |||
415 | log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 415, __func__, "Failed to compress %s: %m" , coredump_tmpfile_name(tmp_compressed)) : -abs(_e); }); | |||
416 | goto fail_compressed; | |||
417 | } | |||
418 | ||||
419 | r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid); | |||
420 | if (r < 0) | |||
421 | goto fail_compressed; | |||
422 | ||||
423 | /* OK, this worked, we can get rid of the uncompressed version now */ | |||
424 | if (tmp) | |||
425 | unlink_noerrno(tmp); | |||
426 | ||||
427 | *ret_filename = TAKE_PTR(fn_compressed)({ typeof(fn_compressed) _ptr_ = (fn_compressed); (fn_compressed ) = ((void*)0); _ptr_; }); /* compressed */ | |||
428 | *ret_node_fd = TAKE_FD(fd_compressed)({ int _fd_ = (fd_compressed); (fd_compressed) = -1; _fd_; }); /* compressed */ | |||
429 | *ret_data_fd = TAKE_FD(fd)({ int _fd_ = (fd); (fd) = -1; _fd_; }); /* uncompressed */ | |||
430 | *ret_size = (uint64_t) st.st_size; /* uncompressed */ | |||
431 | ||||
432 | return 0; | |||
433 | ||||
434 | fail_compressed: | |||
435 | if (tmp_compressed) | |||
436 | (void) unlink(tmp_compressed); | |||
437 | } | |||
438 | ||||
439 | uncompressed: | |||
440 | #endif | |||
441 | ||||
442 | r = fix_permissions(fd, tmp, fn, context, uid); | |||
443 | if (r < 0) | |||
444 | goto fail; | |||
445 | ||||
446 | *ret_filename = TAKE_PTR(fn)({ typeof(fn) _ptr_ = (fn); (fn) = ((void*)0); _ptr_; }); | |||
447 | *ret_data_fd = TAKE_FD(fd)({ int _fd_ = (fd); (fd) = -1; _fd_; }); | |||
448 | *ret_node_fd = -1; | |||
449 | *ret_size = (uint64_t) st.st_size; | |||
450 | ||||
451 | return 0; | |||
452 | ||||
453 | fail: | |||
454 | if (tmp) | |||
455 | (void) unlink(tmp); | |||
456 | return r; | |||
457 | } | |||
458 | ||||
459 | static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) { | |||
460 | _cleanup_free___attribute__((cleanup(freep))) char *field = NULL((void*)0); | |||
461 | ssize_t n; | |||
462 | ||||
463 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/coredump/coredump.c" , 463, __PRETTY_FUNCTION__); } while (0); | |||
464 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/coredump/coredump.c", 464 , __PRETTY_FUNCTION__); } while (0); | |||
465 | assert(ret_size)do { if ((__builtin_expect(!!(!(ret_size)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret_size"), "../src/coredump/coredump.c" , 465, __PRETTY_FUNCTION__); } while (0); | |||
466 | ||||
467 | if (lseek(fd, 0, SEEK_SET0) == (off_t) -1) | |||
468 | return log_warning_errno(errno, "Failed to seek: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 468, __func__ , "Failed to seek: %m") : -abs(_e); }); | |||
469 | ||||
470 | field = malloc(9 + size); | |||
471 | if (!field) { | |||
472 | log_warning("Failed to allocate memory for coredump, coredump will not be stored.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 472, __func__, "Failed to allocate memory for coredump, coredump will not be stored." ) : -abs(_e); }); | |||
473 | return -ENOMEM12; | |||
474 | } | |||
475 | ||||
476 | memcpy(field, "COREDUMP=", 9); | |||
477 | ||||
478 | n = read(fd, field + 9, size); | |||
479 | if (n < 0) | |||
480 | return log_error_errno((int) n, "Failed to read core data: %m")({ int _level = ((3)), _e = (((int) n)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 480, __func__, "Failed to read core data: %m" ) : -abs(_e); }); | |||
481 | if ((size_t) n < size) { | |||
482 | log_error("Core data too short.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 482, __func__, "Core data too short." ) : -abs(_e); }); | |||
483 | return -EIO5; | |||
484 | } | |||
485 | ||||
486 | *ret = TAKE_PTR(field)({ typeof(field) _ptr_ = (field); (field) = ((void*)0); _ptr_ ; }); | |||
487 | *ret_size = size + 9; | |||
488 | ||||
489 | return 0; | |||
490 | } | |||
491 | ||||
492 | /* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines: | |||
493 | * 0:/dev/pts/23 | |||
494 | * pos: 0 | |||
495 | * flags: 0100002 | |||
496 | * | |||
497 | * 1:/dev/pts/23 | |||
498 | * pos: 0 | |||
499 | * flags: 0100002 | |||
500 | * | |||
501 | * 2:/dev/pts/23 | |||
502 | * pos: 0 | |||
503 | * flags: 0100002 | |||
504 | * EOF | |||
505 | */ | |||
506 | static int compose_open_fds(pid_t pid, char **open_fds) { | |||
507 | _cleanup_closedir___attribute__((cleanup(closedirp))) DIR *proc_fd_dir = NULL((void*)0); | |||
508 | _cleanup_close___attribute__((cleanup(closep))) int proc_fdinfo_fd = -1; | |||
509 | _cleanup_free___attribute__((cleanup(freep))) char *buffer = NULL((void*)0); | |||
510 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *stream = NULL((void*)0); | |||
511 | const char *fddelim = "", *path; | |||
512 | struct dirent *dent = NULL((void*)0); | |||
513 | size_t size = 0; | |||
514 | int r = 0; | |||
515 | ||||
516 | assert(pid >= 0)do { if ((__builtin_expect(!!(!(pid >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("pid >= 0"), "../src/coredump/coredump.c" , 516, __PRETTY_FUNCTION__); } while (0); | |||
517 | assert(open_fds != NULL)do { if ((__builtin_expect(!!(!(open_fds != ((void*)0))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("open_fds != NULL" ), "../src/coredump/coredump.c", 517, __PRETTY_FUNCTION__); } while (0); | |||
518 | ||||
519 | path = procfs_file_alloca(pid, "fd")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "fd"); } else { _r_ = __builtin_alloca ((sizeof ("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof( pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t ) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1 + sizeof("fd")); sprintf((char*) _r_, "/proc/""%" "i""/" "fd" , _pid_); } _r_; }); | |||
520 | proc_fd_dir = opendir(path); | |||
521 | if (!proc_fd_dir) | |||
522 | return -errno(*__errno_location ()); | |||
523 | ||||
524 | proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY0200000|O_NOFOLLOW0400000|O_CLOEXEC02000000|O_PATH010000000); | |||
525 | if (proc_fdinfo_fd < 0) | |||
526 | return -errno(*__errno_location ()); | |||
527 | ||||
528 | stream = open_memstream(&buffer, &size); | |||
529 | if (!stream) | |||
530 | return -ENOMEM12; | |||
531 | ||||
532 | (void) __fsetlocking(stream, FSETLOCKING_BYCALLERFSETLOCKING_BYCALLER); | |||
533 | ||||
534 | FOREACH_DIRENT(dent, proc_fd_dir, return -errno)for ((*__errno_location ()) = 0, dent = readdir(proc_fd_dir); ; (*__errno_location ()) = 0, dent = readdir(proc_fd_dir)) if (!dent) { if ((*__errno_location ()) > 0) { return -(*__errno_location ()); } break; } else if (hidden_or_backup_file((dent)->d_name )) continue; else { | |||
535 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *fdinfo = NULL((void*)0); | |||
536 | _cleanup_free___attribute__((cleanup(freep))) char *fdname = NULL((void*)0); | |||
537 | char line[LINE_MAX2048]; | |||
538 | int fd; | |||
539 | ||||
540 | r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); | |||
541 | if (r < 0) | |||
542 | return r; | |||
543 | ||||
544 | fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname); | |||
545 | fddelim = "\n"; | |||
546 | ||||
547 | /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */ | |||
548 | fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW0400000|O_CLOEXEC02000000|O_RDONLY00); | |||
549 | if (fd < 0) | |||
550 | continue; | |||
551 | ||||
552 | fdinfo = fdopen(fd, "re"); | |||
553 | if (!fdinfo) { | |||
554 | safe_close(fd); | |||
555 | continue; | |||
556 | } | |||
557 | ||||
558 | FOREACH_LINE(line, fdinfo, break)for (;;) if (!fgets(line, sizeof(line), fdinfo)) { if (ferror (fdinfo)) { break; } break; } else { | |||
559 | fputs(line, stream); | |||
560 | if (!endswith(line, "\n")) | |||
561 | fputc('\n', stream); | |||
562 | } | |||
563 | } | |||
564 | ||||
565 | errno(*__errno_location ()) = 0; | |||
566 | stream = safe_fclose(stream); | |||
567 | ||||
568 | if (errno(*__errno_location ()) > 0) | |||
569 | return -errno(*__errno_location ()); | |||
570 | ||||
571 | *open_fds = TAKE_PTR(buffer)({ typeof(buffer) _ptr_ = (buffer); (buffer) = ((void*)0); _ptr_ ; }); | |||
572 | ||||
573 | return 0; | |||
574 | } | |||
575 | ||||
576 | static int get_process_ns(pid_t pid, const char *namespace, ino_t *ns) { | |||
577 | const char *p; | |||
578 | struct stat stbuf; | |||
579 | _cleanup_close___attribute__((cleanup(closep))) int proc_ns_dir_fd; | |||
580 | ||||
581 | p = procfs_file_alloca(pid, "ns")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "ns"); } else { _r_ = __builtin_alloca ((sizeof ("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof( pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t ) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1 + sizeof("ns")); sprintf((char*) _r_, "/proc/""%" "i""/" "ns" , _pid_); } _r_; }); | |||
582 | ||||
583 | proc_ns_dir_fd = open(p, O_DIRECTORY0200000 | O_CLOEXEC02000000 | O_RDONLY00); | |||
584 | if (proc_ns_dir_fd < 0) | |||
585 | return -errno(*__errno_location ()); | |||
586 | ||||
587 | if (fstatat(proc_ns_dir_fd, namespace, &stbuf, /* flags */0) < 0) | |||
588 | return -errno(*__errno_location ()); | |||
589 | ||||
590 | *ns = stbuf.st_ino; | |||
591 | return 0; | |||
592 | } | |||
593 | ||||
594 | static int get_mount_namespace_leader(pid_t pid, pid_t *container_pid) { | |||
595 | pid_t cpid = pid, ppid = 0; | |||
596 | ino_t proc_mntns; | |||
597 | int r = 0; | |||
598 | ||||
599 | r = get_process_ns(pid, "mnt", &proc_mntns); | |||
600 | if (r
| |||
601 | return r; | |||
602 | ||||
603 | for (;;) { | |||
604 | ino_t parent_mntns; | |||
605 | ||||
606 | r = get_process_ppid(cpid, &ppid); | |||
607 | if (r < 0) | |||
608 | return r; | |||
609 | ||||
610 | r = get_process_ns(ppid, "mnt", &parent_mntns); | |||
611 | if (r < 0) | |||
612 | return r; | |||
613 | ||||
614 | if (proc_mntns != parent_mntns) | |||
| ||||
615 | break; | |||
616 | ||||
617 | if (ppid == 1) | |||
618 | return -ENOENT2; | |||
619 | ||||
620 | cpid = ppid; | |||
621 | } | |||
622 | ||||
623 | *container_pid = ppid; | |||
624 | return 0; | |||
625 | } | |||
626 | ||||
627 | /* Returns 1 if the parent was found. | |||
628 | * Returns 0 if there is not a process we can call the pid's | |||
629 | * container parent (the pid's process isn't 'containerized'). | |||
630 | * Returns a negative number on errors. | |||
631 | */ | |||
632 | static int get_process_container_parent_cmdline(pid_t pid, char** cmdline) { | |||
633 | int r = 0; | |||
634 | pid_t container_pid; | |||
635 | const char *proc_root_path; | |||
636 | struct stat root_stat, proc_root_stat; | |||
637 | ||||
638 | /* To compare inodes of / and /proc/[pid]/root */ | |||
639 | if (stat("/", &root_stat) < 0) | |||
640 | return -errno(*__errno_location ()); | |||
641 | ||||
642 | proc_root_path = procfs_file_alloca(pid, "root")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "root"); } else { _r_ = __builtin_alloca (( sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof (pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t ) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1 + sizeof("root")); sprintf((char*) _r_, "/proc/""%" "i""/" "root" , _pid_); } _r_; }); | |||
643 | if (stat(proc_root_path, &proc_root_stat) < 0) | |||
644 | return -errno(*__errno_location ()); | |||
645 | ||||
646 | /* The process uses system root. */ | |||
647 | if (proc_root_stat.st_ino == root_stat.st_ino) { | |||
648 | *cmdline = NULL((void*)0); | |||
649 | return 0; | |||
650 | } | |||
651 | ||||
652 | r = get_mount_namespace_leader(pid, &container_pid); | |||
653 | if (r < 0) | |||
654 | return r; | |||
655 | ||||
656 | r = get_process_cmdline(container_pid, 0, false0, cmdline); | |||
657 | if (r < 0) | |||
658 | return r; | |||
659 | ||||
660 | return 1; | |||
661 | } | |||
662 | ||||
663 | static int change_uid_gid(const char *context[]) { | |||
664 | uid_t uid; | |||
665 | gid_t gid; | |||
666 | int r; | |||
667 | ||||
668 | r = parse_uid(context[CONTEXT_UID], &uid); | |||
669 | if (r < 0) | |||
670 | return r; | |||
671 | ||||
672 | if (uid <= SYSTEM_UID_MAX999) { | |||
673 | const char *user = "systemd-coredump"; | |||
674 | ||||
675 | r = get_user_creds(&user, &uid, &gid, NULL((void*)0), NULL((void*)0)); | |||
676 | if (r < 0) { | |||
677 | log_warning_errno(r, "Cannot resolve %s user. Proceeding to dump core as root: %m", user)({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 677, __func__, "Cannot resolve %s user. Proceeding to dump core as root: %m" , user) : -abs(_e); }); | |||
678 | uid = gid = 0; | |||
679 | } | |||
680 | } else { | |||
681 | r = parse_gid(context[CONTEXT_GID], &gid); | |||
682 | if (r < 0) | |||
683 | return r; | |||
684 | } | |||
685 | ||||
686 | return drop_privileges(uid, gid, 0); | |||
687 | } | |||
688 | ||||
689 | static bool_Bool is_journald_crash(const char *context[_CONTEXT_MAX]) { | |||
690 | assert(context)do { if ((__builtin_expect(!!(!(context)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context"), "../src/coredump/coredump.c" , 690, __PRETTY_FUNCTION__); } while (0); | |||
691 | ||||
692 | return streq_ptr(context[CONTEXT_UNIT], SPECIAL_JOURNALD_SERVICE"systemd-journald.service"); | |||
693 | } | |||
694 | ||||
695 | static bool_Bool is_pid1_crash(const char *context[_CONTEXT_MAX]) { | |||
696 | assert(context)do { if ((__builtin_expect(!!(!(context)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context"), "../src/coredump/coredump.c" , 696, __PRETTY_FUNCTION__); } while (0); | |||
697 | ||||
698 | return streq_ptr(context[CONTEXT_UNIT], SPECIAL_INIT_SCOPE"init.scope") || | |||
699 | streq_ptr(context[CONTEXT_PID], "1"); | |||
700 | } | |||
701 | ||||
702 | #define SUBMIT_COREDUMP_FIELDS4 4 | |||
703 | ||||
704 | static int submit_coredump( | |||
705 | const char *context[_CONTEXT_MAX], | |||
706 | struct iovec *iovec, | |||
707 | size_t n_iovec_allocated, | |||
708 | size_t n_iovec, | |||
709 | int input_fd) { | |||
710 | ||||
711 | _cleanup_close___attribute__((cleanup(closep))) int coredump_fd = -1, coredump_node_fd = -1; | |||
712 | _cleanup_free___attribute__((cleanup(freep))) char *core_message = NULL((void*)0), *filename = NULL((void*)0), *coredump_data = NULL((void*)0); | |||
713 | uint64_t coredump_size = UINT64_MAX(18446744073709551615UL); | |||
714 | bool_Bool truncated = false0, journald_crash; | |||
715 | int r; | |||
716 | ||||
717 | assert(context)do { if ((__builtin_expect(!!(!(context)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context"), "../src/coredump/coredump.c" , 717, __PRETTY_FUNCTION__); } while (0); | |||
718 | assert(iovec)do { if ((__builtin_expect(!!(!(iovec)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("iovec"), "../src/coredump/coredump.c", 718 , __PRETTY_FUNCTION__); } while (0); | |||
719 | assert(n_iovec_allocated >= n_iovec + SUBMIT_COREDUMP_FIELDS)do { if ((__builtin_expect(!!(!(n_iovec_allocated >= n_iovec + 4)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("n_iovec_allocated >= n_iovec + SUBMIT_COREDUMP_FIELDS" ), "../src/coredump/coredump.c", 719, __PRETTY_FUNCTION__); } while (0); | |||
720 | assert(input_fd >= 0)do { if ((__builtin_expect(!!(!(input_fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("input_fd >= 0"), "../src/coredump/coredump.c" , 720, __PRETTY_FUNCTION__); } while (0); | |||
721 | ||||
722 | journald_crash = is_journald_crash(context); | |||
723 | ||||
724 | /* Vacuum before we write anything again */ | |||
725 | (void) coredump_vacuum(-1, arg_keep_free, arg_max_use); | |||
726 | ||||
727 | /* Always stream the coredump to disk, if that's possible */ | |||
728 | r = save_external_coredump(context, input_fd, | |||
729 | &filename, &coredump_node_fd, &coredump_fd, &coredump_size, &truncated); | |||
730 | if (r < 0) | |||
731 | /* Skip whole core dumping part */ | |||
732 | goto log; | |||
733 | ||||
734 | /* If we don't want to keep the coredump on disk, remove it now, as later on we will lack the privileges for | |||
735 | * it. However, we keep the fd to it, so that we can still process it and log it. */ | |||
736 | r = maybe_remove_external_coredump(filename, coredump_size); | |||
737 | if (r < 0) | |||
738 | return r; | |||
739 | if (r == 0) { | |||
740 | const char *coredump_filename; | |||
741 | ||||
742 | coredump_filename = strjoina("COREDUMP_FILENAME=", filename)({ const char *_appendees_[] = { "COREDUMP_FILENAME=", filename }; char *_d_, *_p_; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_ )/sizeof((_appendees_)[0]), ((void)0))) && _appendees_ [_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca (_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(_appendees_), typeof(& *(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0] ), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy (_p_, _appendees_[_i_]); *_p_ = 0; _d_; }); | |||
743 | iovec[n_iovec++] = IOVEC_MAKE_STRING(coredump_filename)(struct iovec) { .iov_base = ((char*) coredump_filename), .iov_len = (strlen(coredump_filename)) }; | |||
744 | } else if (arg_storage == COREDUMP_STORAGE_EXTERNAL) | |||
745 | log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 746, __func__, "The core will not be stored: size %" "l" "u"" is greater than %""l" "u"" (the configured maximum)" , coredump_size, arg_external_size_max) : -abs(_e); }) | |||
746 | coredump_size, arg_external_size_max)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 746, __func__, "The core will not be stored: size %" "l" "u"" is greater than %""l" "u"" (the configured maximum)" , coredump_size, arg_external_size_max) : -abs(_e); }); | |||
747 | ||||
748 | /* Vacuum again, but exclude the coredump we just created */ | |||
749 | (void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use); | |||
750 | ||||
751 | /* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump | |||
752 | * memory under the user's uid. This also ensures that the credentials journald will see are the ones of the | |||
753 | * coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all | |||
754 | * capabilities, if we run as root, we won't need them anymore. */ | |||
755 | r = change_uid_gid(context); | |||
756 | if (r < 0) | |||
757 | return log_error_errno(r, "Failed to drop privileges: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 757, __func__, "Failed to drop privileges: %m" ) : -abs(_e); }); | |||
758 | ||||
759 | #if HAVE_ELFUTILS1 | |||
760 | /* Try to get a strack trace if we can */ | |||
761 | if (coredump_size <= arg_process_size_max) { | |||
762 | _cleanup_free___attribute__((cleanup(freep))) char *stacktrace = NULL((void*)0); | |||
763 | ||||
764 | r = coredump_make_stack_trace(coredump_fd, context[CONTEXT_EXE], &stacktrace); | |||
765 | if (r >= 0) | |||
766 | core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash ? "\nCoredump diverted to " : "", journald_crash ? filename : "", "\n\n", stacktrace, ((void*)0)) | |||
767 | " (", context[CONTEXT_COMM], ") of user ",strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash ? "\nCoredump diverted to " : "", journald_crash ? filename : "", "\n\n", stacktrace, ((void*)0)) | |||
768 | context[CONTEXT_UID], " dumped core.",strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash ? "\nCoredump diverted to " : "", journald_crash ? filename : "", "\n\n", stacktrace, ((void*)0)) | |||
769 | journald_crash ? "\nCoredump diverted to " : "",strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash ? "\nCoredump diverted to " : "", journald_crash ? filename : "", "\n\n", stacktrace, ((void*)0)) | |||
770 | journald_crash ? filename : "",strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash ? "\nCoredump diverted to " : "", journald_crash ? filename : "", "\n\n", stacktrace, ((void*)0)) | |||
771 | "\n\n", stacktrace)strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash ? "\nCoredump diverted to " : "", journald_crash ? filename : "", "\n\n", stacktrace, ((void*)0)); | |||
772 | else if (r == -EINVAL22) | |||
773 | log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()))({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 773, __func__, "Failed to generate stack trace: %s" , dwfl_errmsg(dwfl_errno())) : -abs(_e); }); | |||
774 | else | |||
775 | log_warning_errno(r, "Failed to generate stack trace: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 775, __func__, "Failed to generate stack trace: %m" ) : -abs(_e); }); | |||
776 | } else | |||
777 | log_debug("Not generating stack trace: core size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 778, __func__, "Not generating stack trace: core size %" "l" "u"" is greater than %""l" "u"" (the configured maximum)" , coredump_size, arg_process_size_max) : -abs(_e); }) | |||
778 | coredump_size, arg_process_size_max)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 778, __func__, "Not generating stack trace: core size %" "l" "u"" is greater than %""l" "u"" (the configured maximum)" , coredump_size, arg_process_size_max) : -abs(_e); }); | |||
779 | ||||
780 | if (!core_message) | |||
781 | #endif | |||
782 | log: | |||
783 | core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash && filename ? "\nCoredump diverted to " : ((void*)0), journald_crash && filename ? filename : ((void*)0), ((void*)0)) | |||
784 | " (", context[CONTEXT_COMM], ") of user ",strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash && filename ? "\nCoredump diverted to " : ((void*)0), journald_crash && filename ? filename : ((void*)0), ((void*)0)) | |||
785 | context[CONTEXT_UID], " dumped core.",strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash && filename ? "\nCoredump diverted to " : ((void*)0), journald_crash && filename ? filename : ((void*)0), ((void*)0)) | |||
786 | journald_crash && filename ? "\nCoredump diverted to " : NULL,strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash && filename ? "\nCoredump diverted to " : ((void*)0), journald_crash && filename ? filename : ((void*)0), ((void*)0)) | |||
787 | journald_crash && filename ? filename : NULL)strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core." , journald_crash && filename ? "\nCoredump diverted to " : ((void*)0), journald_crash && filename ? filename : ((void*)0), ((void*)0)); | |||
788 | if (!core_message) | |||
789 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 789, __func__); | |||
790 | ||||
791 | if (journald_crash) { | |||
792 | /* We cannot log to the journal, so just print the message. | |||
793 | * The target was set previously to something safe. */ | |||
794 | assert(startswith(core_message, "MESSAGE="))do { if ((__builtin_expect(!!(!(startswith(core_message, "MESSAGE=" ))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("startswith(core_message, \"MESSAGE=\")" ), "../src/coredump/coredump.c", 794, __PRETTY_FUNCTION__); } while (0); | |||
795 | log_dispatch(LOG_ERR, 0, core_message + strlen("MESSAGE="))log_dispatch_internal(3, 0, "../src/coredump/coredump.c", 795 , __func__, ((void*)0), ((void*)0), ((void*)0), ((void*)0), core_message + strlen("MESSAGE=")); | |||
796 | return 0; | |||
797 | } | |||
798 | ||||
799 | iovec[n_iovec++] = IOVEC_MAKE_STRING(core_message)(struct iovec) { .iov_base = ((char*) core_message), .iov_len = (strlen(core_message)) }; | |||
800 | ||||
801 | if (truncated) | |||
802 | iovec[n_iovec++] = IOVEC_MAKE_STRING("COREDUMP_TRUNCATED=1")(struct iovec) { .iov_base = ((char*) "COREDUMP_TRUNCATED=1") , .iov_len = (strlen("COREDUMP_TRUNCATED=1")) }; | |||
803 | ||||
804 | /* Optionally store the entire coredump in the journal */ | |||
805 | if (arg_storage == COREDUMP_STORAGE_JOURNAL) { | |||
806 | if (coredump_size <= arg_journal_size_max) { | |||
807 | size_t sz = 0; | |||
808 | ||||
809 | /* Store the coredump itself in the journal */ | |||
810 | ||||
811 | r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz); | |||
812 | if (r >= 0) | |||
813 | iovec[n_iovec++] = IOVEC_MAKE(coredump_data, sz)(struct iovec) { .iov_base = (coredump_data), .iov_len = (sz) }; | |||
814 | else | |||
815 | log_warning_errno(r, "Failed to attach the core to the journal entry: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 815, __func__, "Failed to attach the core to the journal entry: %m" ) : -abs(_e); }); | |||
816 | } else | |||
817 | log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 818, __func__, "The core will not be stored: size %" "l" "u"" is greater than %""l" "u"" (the configured maximum)" , coredump_size, arg_journal_size_max) : -abs(_e); }) | |||
818 | coredump_size, arg_journal_size_max)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 818, __func__, "The core will not be stored: size %" "l" "u"" is greater than %""l" "u"" (the configured maximum)" , coredump_size, arg_journal_size_max) : -abs(_e); }); | |||
819 | } | |||
820 | ||||
821 | assert(n_iovec <= n_iovec_allocated)do { if ((__builtin_expect(!!(!(n_iovec <= n_iovec_allocated )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("n_iovec <= n_iovec_allocated" ), "../src/coredump/coredump.c", 821, __PRETTY_FUNCTION__); } while (0); | |||
822 | ||||
823 | r = sd_journal_sendv(iovec, n_iovec)sd_journal_sendv_with_location("CODE_FILE=" "../src/coredump/coredump.c" , "CODE_LINE=" "823", __func__, iovec, n_iovec); | |||
824 | if (r < 0) | |||
825 | return log_error_errno(r, "Failed to log coredump: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 825, __func__, "Failed to log coredump: %m" ) : -abs(_e); }); | |||
826 | ||||
827 | return 0; | |||
828 | } | |||
829 | ||||
830 | static void map_context_fields(const struct iovec *iovec, const char* context[]) { | |||
831 | ||||
832 | static const char * const context_field_names[] = { | |||
833 | [CONTEXT_PID] = "COREDUMP_PID=", | |||
834 | [CONTEXT_UID] = "COREDUMP_UID=", | |||
835 | [CONTEXT_GID] = "COREDUMP_GID=", | |||
836 | [CONTEXT_SIGNAL] = "COREDUMP_SIGNAL=", | |||
837 | [CONTEXT_TIMESTAMP] = "COREDUMP_TIMESTAMP=", | |||
838 | [CONTEXT_RLIMIT] = "COREDUMP_RLIMIT=", | |||
839 | [CONTEXT_HOSTNAME] = "COREDUMP_HOSTNAME=", | |||
840 | [CONTEXT_COMM] = "COREDUMP_COMM=", | |||
841 | [CONTEXT_EXE] = "COREDUMP_EXE=", | |||
842 | }; | |||
843 | ||||
844 | unsigned i; | |||
845 | ||||
846 | assert(iovec)do { if ((__builtin_expect(!!(!(iovec)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("iovec"), "../src/coredump/coredump.c", 846 , __PRETTY_FUNCTION__); } while (0); | |||
847 | assert(context)do { if ((__builtin_expect(!!(!(context)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context"), "../src/coredump/coredump.c" , 847, __PRETTY_FUNCTION__); } while (0); | |||
848 | ||||
849 | for (i = 0; i < ELEMENTSOF(context_field_names)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(context_field_names), typeof(&*(context_field_names ))), sizeof(context_field_names)/sizeof((context_field_names) [0]), ((void)0))); i++) { | |||
850 | char *p; | |||
851 | ||||
852 | if (!context_field_names[i]) | |||
853 | continue; | |||
854 | ||||
855 | p = memory_startswith(iovec->iov_base, iovec->iov_len, context_field_names[i]); | |||
856 | if (!p) | |||
857 | continue; | |||
858 | ||||
859 | /* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the | |||
860 | * buffer, though not included in the iov_len count. (see below) */ | |||
861 | context[i] = p; | |||
862 | break; | |||
863 | } | |||
864 | } | |||
865 | ||||
866 | static int process_socket(int fd) { | |||
867 | _cleanup_close___attribute__((cleanup(closep))) int coredump_fd = -1; | |||
868 | struct iovec *iovec = NULL((void*)0); | |||
869 | size_t n_iovec = 0, n_allocated = 0, i, k; | |||
870 | const char *context[_CONTEXT_MAX] = {}; | |||
871 | int r; | |||
872 | ||||
873 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/coredump/coredump.c" , 873, __PRETTY_FUNCTION__); } while (0); | |||
874 | ||||
875 | log_set_target(LOG_TARGET_AUTO); | |||
876 | log_parse_environment()log_parse_environment_realm(LOG_REALM_SYSTEMD); | |||
877 | log_open(); | |||
878 | ||||
879 | log_debug("Processing coredump received on stdin...")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 879, __func__, "Processing coredump received on stdin..." ) : -abs(_e); }); | |||
880 | ||||
881 | for (;;) { | |||
882 | union { | |||
883 | struct cmsghdr cmsghdr; | |||
884 | uint8_t buf[CMSG_SPACE(sizeof(int))((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t ) - 1) & (size_t) ~(sizeof (size_t) - 1)))]; | |||
885 | } control = {}; | |||
886 | struct msghdr mh = { | |||
887 | .msg_control = &control, | |||
888 | .msg_controllen = sizeof(control), | |||
889 | .msg_iovlen = 1, | |||
890 | }; | |||
891 | ssize_t n; | |||
892 | ssize_t l; | |||
893 | ||||
894 | if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + SUBMIT_COREDUMP_FIELDS)greedy_realloc((void**) &(iovec), &(n_allocated), (n_iovec + 4), sizeof((iovec)[0]))) { | |||
895 | r = log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 895, __func__); | |||
896 | goto finish; | |||
897 | } | |||
898 | ||||
899 | l = next_datagram_size_fd(fd); | |||
900 | if (l < 0) { | |||
901 | r = log_error_errno(l, "Failed to determine datagram size to read: %m")({ int _level = ((3)), _e = ((l)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 901, __func__, "Failed to determine datagram size to read: %m" ) : -abs(_e); }); | |||
902 | goto finish; | |||
903 | } | |||
904 | ||||
905 | assert(l >= 0)do { if ((__builtin_expect(!!(!(l >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("l >= 0"), "../src/coredump/coredump.c" , 905, __PRETTY_FUNCTION__); } while (0); | |||
906 | ||||
907 | iovec[n_iovec].iov_len = l; | |||
908 | iovec[n_iovec].iov_base = malloc(l + 1); | |||
909 | if (!iovec[n_iovec].iov_base) { | |||
910 | r = log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 910, __func__); | |||
911 | goto finish; | |||
912 | } | |||
913 | ||||
914 | mh.msg_iov = iovec + n_iovec; | |||
915 | ||||
916 | n = recvmsg(fd, &mh, MSG_CMSG_CLOEXECMSG_CMSG_CLOEXEC); | |||
917 | if (n < 0) { | |||
918 | free(iovec[n_iovec].iov_base); | |||
919 | r = log_error_errno(errno, "Failed to receive datagram: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 919, __func__ , "Failed to receive datagram: %m") : -abs(_e); }); | |||
920 | goto finish; | |||
921 | } | |||
922 | ||||
923 | if (n == 0) { | |||
924 | struct cmsghdr *cmsg, *found = NULL((void*)0); | |||
925 | /* The final zero-length datagram carries the file descriptor and tells us that we're done. */ | |||
926 | ||||
927 | free(iovec[n_iovec].iov_base); | |||
928 | ||||
929 | CMSG_FOREACH(cmsg, &mh)for ((cmsg) = ((size_t) (&mh)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr *) 0); (cmsg); (cmsg) = __cmsg_nxthdr ((& mh), (cmsg))) { | |||
930 | if (cmsg->cmsg_level == SOL_SOCKET1 && | |||
931 | cmsg->cmsg_type == SCM_RIGHTSSCM_RIGHTS && | |||
932 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t ) ~(sizeof (size_t) - 1)) + (sizeof(int)))) { | |||
933 | assert(!found)do { if ((__builtin_expect(!!(!(!found)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!found"), "../src/coredump/coredump.c", 933, __PRETTY_FUNCTION__); } while (0); | |||
934 | found = cmsg; | |||
935 | } | |||
936 | } | |||
937 | ||||
938 | if (!found) { | |||
939 | log_error("Coredump file descriptor missing.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 939, __func__, "Coredump file descriptor missing." ) : -abs(_e); }); | |||
940 | r = -EBADMSG74; | |||
941 | goto finish; | |||
942 | } | |||
943 | ||||
944 | assert(coredump_fd < 0)do { if ((__builtin_expect(!!(!(coredump_fd < 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("coredump_fd < 0"), "../src/coredump/coredump.c" , 944, __PRETTY_FUNCTION__); } while (0); | |||
945 | coredump_fd = *(int*) CMSG_DATA(found)((found)->__cmsg_data); | |||
946 | break; | |||
947 | } | |||
948 | ||||
949 | /* Add trailing NUL byte, in case these are strings */ | |||
950 | ((char*) iovec[n_iovec].iov_base)[n] = 0; | |||
951 | iovec[n_iovec].iov_len = (size_t) n; | |||
952 | ||||
953 | cmsg_close_all(&mh); | |||
954 | map_context_fields(iovec + n_iovec, context); | |||
955 | n_iovec++; | |||
956 | } | |||
957 | ||||
958 | if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + SUBMIT_COREDUMP_FIELDS)greedy_realloc((void**) &(iovec), &(n_allocated), (n_iovec + 4), sizeof((iovec)[0]))) { | |||
959 | r = log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 959, __func__); | |||
960 | goto finish; | |||
961 | } | |||
962 | ||||
963 | /* Make sure we got all data we really need */ | |||
964 | assert(context[CONTEXT_PID])do { if ((__builtin_expect(!!(!(context[CONTEXT_PID])),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context[CONTEXT_PID]"), "../src/coredump/coredump.c" , 964, __PRETTY_FUNCTION__); } while (0); | |||
965 | assert(context[CONTEXT_UID])do { if ((__builtin_expect(!!(!(context[CONTEXT_UID])),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context[CONTEXT_UID]"), "../src/coredump/coredump.c" , 965, __PRETTY_FUNCTION__); } while (0); | |||
966 | assert(context[CONTEXT_GID])do { if ((__builtin_expect(!!(!(context[CONTEXT_GID])),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context[CONTEXT_GID]"), "../src/coredump/coredump.c" , 966, __PRETTY_FUNCTION__); } while (0); | |||
967 | assert(context[CONTEXT_SIGNAL])do { if ((__builtin_expect(!!(!(context[CONTEXT_SIGNAL])),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("context[CONTEXT_SIGNAL]" ), "../src/coredump/coredump.c", 967, __PRETTY_FUNCTION__); } while (0); | |||
968 | assert(context[CONTEXT_TIMESTAMP])do { if ((__builtin_expect(!!(!(context[CONTEXT_TIMESTAMP])), 0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("context[CONTEXT_TIMESTAMP]" ), "../src/coredump/coredump.c", 968, __PRETTY_FUNCTION__); } while (0); | |||
969 | assert(context[CONTEXT_RLIMIT])do { if ((__builtin_expect(!!(!(context[CONTEXT_RLIMIT])),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("context[CONTEXT_RLIMIT]" ), "../src/coredump/coredump.c", 969, __PRETTY_FUNCTION__); } while (0); | |||
970 | assert(context[CONTEXT_HOSTNAME])do { if ((__builtin_expect(!!(!(context[CONTEXT_HOSTNAME])),0 ))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("context[CONTEXT_HOSTNAME]" ), "../src/coredump/coredump.c", 970, __PRETTY_FUNCTION__); } while (0); | |||
971 | assert(context[CONTEXT_COMM])do { if ((__builtin_expect(!!(!(context[CONTEXT_COMM])),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("context[CONTEXT_COMM]"), "../src/coredump/coredump.c" , 971, __PRETTY_FUNCTION__); } while (0); | |||
972 | assert(coredump_fd >= 0)do { if ((__builtin_expect(!!(!(coredump_fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("coredump_fd >= 0"), "../src/coredump/coredump.c" , 972, __PRETTY_FUNCTION__); } while (0); | |||
973 | ||||
974 | /* Small quirk: the journal fields contain the timestamp padded with six zeroes, so that the kernel-supplied 1s | |||
975 | * granularity timestamps becomes 1µs granularity, i.e. the granularity systemd usually operates in. Since we | |||
976 | * are reconstructing the original kernel context, we chop this off again, here. */ | |||
977 | k = strlen(context[CONTEXT_TIMESTAMP]); | |||
978 | if (k > 6) | |||
979 | context[CONTEXT_TIMESTAMP] = strndupa(context[CONTEXT_TIMESTAMP], k - 6)(__extension__ ({ const char *__old = (context[CONTEXT_TIMESTAMP ]); size_t __len = strnlen (__old, (k - 6)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char * ) memcpy (__new, __old, __len); })); | |||
980 | ||||
981 | r = submit_coredump(context, iovec, n_allocated, n_iovec, coredump_fd); | |||
982 | ||||
983 | finish: | |||
984 | for (i = 0; i < n_iovec; i++) | |||
985 | free(iovec[i].iov_base); | |||
986 | free(iovec); | |||
987 | ||||
988 | return r; | |||
989 | } | |||
990 | ||||
991 | static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd) { | |||
992 | ||||
993 | static const union sockaddr_union sa = { | |||
994 | .un.sun_family = AF_UNIX1, | |||
995 | .un.sun_path = "/run/systemd/coredump", | |||
996 | }; | |||
997 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; | |||
998 | size_t i; | |||
999 | int r; | |||
1000 | ||||
1001 | assert(iovec || n_iovec <= 0)do { if ((__builtin_expect(!!(!(iovec || n_iovec <= 0)),0) )) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("iovec || n_iovec <= 0" ), "../src/coredump/coredump.c", 1001, __PRETTY_FUNCTION__); } while (0); | |||
1002 | assert(input_fd >= 0)do { if ((__builtin_expect(!!(!(input_fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("input_fd >= 0"), "../src/coredump/coredump.c" , 1002, __PRETTY_FUNCTION__); } while (0); | |||
1003 | ||||
1004 | fd = socket(AF_UNIX1, SOCK_SEQPACKETSOCK_SEQPACKET|SOCK_CLOEXECSOCK_CLOEXEC, 0); | |||
1005 | if (fd < 0) | |||
1006 | return log_error_errno(errno, "Failed to create coredump socket: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 1006, __func__ , "Failed to create coredump socket: %m") : -abs(_e); }); | |||
1007 | ||||
1008 | if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)({ const struct sockaddr_un *_sa = &(sa.un); do { if ((__builtin_expect (!!(!(_sa->sun_family == 1)),0))) log_assert_failed_realm( LOG_REALM_SYSTEMD, ("_sa->sun_family == AF_UNIX"), "../src/coredump/coredump.c" , 1008, __PRETTY_FUNCTION__); } while (0); __builtin_offsetof (struct sockaddr_un, sun_path) + (_sa->sun_path[0] == 0 ? 1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : strnlen (_sa->sun_path, sizeof(_sa->sun_path))); })) < 0) | |||
1009 | return log_error_errno(errno, "Failed to connect to coredump service: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 1009, __func__ , "Failed to connect to coredump service: %m") : -abs(_e); }); | |||
1010 | ||||
1011 | for (i = 0; i < n_iovec; i++) { | |||
1012 | struct msghdr mh = { | |||
1013 | .msg_iov = (struct iovec*) iovec + i, | |||
1014 | .msg_iovlen = 1, | |||
1015 | }; | |||
1016 | struct iovec copy[2]; | |||
1017 | ||||
1018 | for (;;) { | |||
1019 | if (sendmsg(fd, &mh, MSG_NOSIGNALMSG_NOSIGNAL) >= 0) | |||
1020 | break; | |||
1021 | ||||
1022 | if (errno(*__errno_location ()) == EMSGSIZE90 && mh.msg_iov[0].iov_len > 0) { | |||
1023 | /* This field didn't fit? That's a pity. Given that this is just metadata, | |||
1024 | * let's truncate the field at half, and try again. We append three dots, in | |||
1025 | * order to show that this is truncated. */ | |||
1026 | ||||
1027 | if (mh.msg_iov != copy) { | |||
1028 | /* We don't want to modify the caller's iovec, hence let's create our | |||
1029 | * own array, consisting of two new iovecs, where the first is a | |||
1030 | * (truncated) copy of what we want to send, and the second one | |||
1031 | * contains the trailing dots. */ | |||
1032 | copy[0] = iovec[i]; | |||
1033 | copy[1] = (struct iovec) { | |||
1034 | .iov_base = (char[]) { '.', '.', '.' }, | |||
1035 | .iov_len = 3, | |||
1036 | }; | |||
1037 | ||||
1038 | mh.msg_iov = copy; | |||
1039 | mh.msg_iovlen = 2; | |||
1040 | } | |||
1041 | ||||
1042 | copy[0].iov_len /= 2; /* halve it, and try again */ | |||
1043 | continue; | |||
1044 | } | |||
1045 | ||||
1046 | return log_error_errno(errno, "Failed to send coredump datagram: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/coredump/coredump.c", 1046, __func__ , "Failed to send coredump datagram: %m") : -abs(_e); }); | |||
1047 | } | |||
1048 | } | |||
1049 | ||||
1050 | r = send_one_fd(fd, input_fd, 0)send_one_fd_iov_sa(fd, input_fd, ((void*)0), 0, ((void*)0), 0 , 0); | |||
1051 | if (r < 0) | |||
1052 | return log_error_errno(r, "Failed to send coredump fd: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1052, __func__, "Failed to send coredump fd: %m" ) : -abs(_e); }); | |||
1053 | ||||
1054 | return 0; | |||
1055 | } | |||
1056 | ||||
1057 | static char* set_iovec_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) { | |||
1058 | char *x; | |||
1059 | ||||
1060 | x = set_iovec_string_field(iovec, n_iovec, field, value); | |||
1061 | free(value); | |||
1062 | return x; | |||
1063 | } | |||
1064 | ||||
1065 | static int gather_pid_metadata( | |||
1066 | char* context[_CONTEXT_MAX], | |||
1067 | char **comm_fallback, | |||
1068 | struct iovec *iovec, size_t *n_iovec) { | |||
1069 | ||||
1070 | /* We need 27 empty slots in iovec! | |||
1071 | * | |||
1072 | * Note that if we fail on oom later on, we do not roll-back changes to the iovec structure. (It remains valid, | |||
1073 | * with the first n_iovec fields initialized.) */ | |||
1074 | ||||
1075 | uid_t owner_uid; | |||
1076 | pid_t pid; | |||
1077 | char *t; | |||
1078 | const char *p; | |||
1079 | int r, signo; | |||
1080 | ||||
1081 | r = parse_pid(context[CONTEXT_PID], &pid); | |||
1082 | if (r < 0) | |||
| ||||
1083 | return log_error_errno(r, "Failed to parse PID \"%s\": %m", context[CONTEXT_PID])({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1083, __func__, "Failed to parse PID \"%s\": %m" , context[CONTEXT_PID]) : -abs(_e); }); | |||
1084 | ||||
1085 | r = get_process_comm(pid, &context[CONTEXT_COMM]); | |||
1086 | if (r < 0) { | |||
1087 | log_warning_errno(r, "Failed to get COMM, falling back to the command line: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1087, __func__, "Failed to get COMM, falling back to the command line: %m" ) : -abs(_e); }); | |||
1088 | context[CONTEXT_COMM] = strv_join(comm_fallback, " "); | |||
1089 | if (!context[CONTEXT_COMM]) | |||
1090 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1090, __func__); | |||
1091 | } | |||
1092 | ||||
1093 | r = get_process_exe(pid, &context[CONTEXT_EXE]); | |||
1094 | if (r < 0) | |||
1095 | log_warning_errno(r, "Failed to get EXE, ignoring: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1095, __func__, "Failed to get EXE, ignoring: %m" ) : -abs(_e); }); | |||
1096 | ||||
1097 | if (cg_pid_get_unit(pid, &context[CONTEXT_UNIT]) >= 0) { | |||
1098 | if (!is_journald_crash((const char**) context)) { | |||
1099 | /* OK, now we know it's not the journal, hence we can make use of it now. */ | |||
1100 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |||
1101 | log_open(); | |||
1102 | } | |||
1103 | ||||
1104 | /* If this is PID 1 disable coredump collection, we'll unlikely be able to process it later on. */ | |||
1105 | if (is_pid1_crash((const char**) context)) { | |||
1106 | log_notice("Due to PID 1 having crashed coredump collection will now be turned off.")({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1106, __func__, "Due to PID 1 having crashed coredump collection will now be turned off." ) : -abs(_e); }); | |||
1107 | disable_coredumps(); | |||
1108 | } | |||
1109 | ||||
1110 | set_iovec_string_field(iovec, n_iovec, "COREDUMP_UNIT=", context[CONTEXT_UNIT]); | |||
1111 | } | |||
1112 | ||||
1113 | if (cg_pid_get_user_unit(pid, &t) >= 0) | |||
1114 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_USER_UNIT=", t); | |||
1115 | ||||
1116 | /* The next few are mandatory */ | |||
1117 | if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_PID=", context[CONTEXT_PID])) | |||
1118 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1118, __func__); | |||
1119 | ||||
1120 | if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_UID=", context[CONTEXT_UID])) | |||
1121 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1121, __func__); | |||
1122 | ||||
1123 | if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_GID=", context[CONTEXT_GID])) | |||
1124 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1124, __func__); | |||
1125 | ||||
1126 | if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL])) | |||
1127 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1127, __func__); | |||
1128 | ||||
1129 | if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT])) | |||
1130 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1130, __func__); | |||
1131 | ||||
1132 | if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_HOSTNAME=", context[CONTEXT_HOSTNAME])) | |||
1133 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1133, __func__); | |||
1134 | ||||
1135 | if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_COMM=", context[CONTEXT_COMM])) | |||
1136 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1136, __func__); | |||
1137 | ||||
1138 | if (context[CONTEXT_EXE] && | |||
1139 | !set_iovec_string_field(iovec, n_iovec, "COREDUMP_EXE=", context[CONTEXT_EXE])) | |||
1140 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1140, __func__); | |||
1141 | ||||
1142 | if (sd_pid_get_session(pid, &t) >= 0) | |||
1143 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_SESSION=", t); | |||
1144 | ||||
1145 | if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) { | |||
1146 | r = asprintf(&t, "COREDUMP_OWNER_UID=" UID_FMT"%" "u", owner_uid); | |||
1147 | if (r > 0) | |||
1148 | iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t)(struct iovec) { .iov_base = ((char*) t), .iov_len = (strlen( t)) }; | |||
1149 | } | |||
1150 | ||||
1151 | if (sd_pid_get_slice(pid, &t) >= 0) | |||
1152 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_SLICE=", t); | |||
1153 | ||||
1154 | if (get_process_cmdline(pid, 0, false0, &t) >= 0) | |||
1155 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CMDLINE=", t); | |||
1156 | ||||
1157 | if (cg_pid_get_path_shifted(pid, NULL((void*)0), &t) >= 0) | |||
1158 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CGROUP=", t); | |||
1159 | ||||
1160 | if (compose_open_fds(pid, &t) >= 0) | |||
1161 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_OPEN_FDS=", t); | |||
1162 | ||||
1163 | p = procfs_file_alloca(pid, "status")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "status"); } else { _r_ = __builtin_alloca ( (sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof (pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t ) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1 + sizeof("status")); sprintf((char*) _r_, "/proc/""%" "i""/" "status", _pid_); } _r_; }); | |||
1164 | if (read_full_file(p, &t, NULL((void*)0)) >= 0) | |||
1165 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_STATUS=", t); | |||
1166 | ||||
1167 | p = procfs_file_alloca(pid, "maps")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "maps"); } else { _r_ = __builtin_alloca (( sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof (pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t ) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1 + sizeof("maps")); sprintf((char*) _r_, "/proc/""%" "i""/" "maps" , _pid_); } _r_; }); | |||
1168 | if (read_full_file(p, &t, NULL((void*)0)) >= 0) | |||
1169 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_MAPS=", t); | |||
1170 | ||||
1171 | p = procfs_file_alloca(pid, "limits")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "limits"); } else { _r_ = __builtin_alloca ( (sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof (pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t ) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1 + sizeof("limits")); sprintf((char*) _r_, "/proc/""%" "i""/" "limits", _pid_); } _r_; }); | |||
1172 | if (read_full_file(p, &t, NULL((void*)0)) >= 0) | |||
1173 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_LIMITS=", t); | |||
1174 | ||||
1175 | p = procfs_file_alloca(pid, "cgroup")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "cgroup"); } else { _r_ = __builtin_alloca ( (sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof (pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t ) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1 + sizeof("cgroup")); sprintf((char*) _r_, "/proc/""%" "i""/" "cgroup", _pid_); } _r_; }); | |||
1176 | if (read_full_file(p, &t, NULL((void*)0)) >=0) | |||
1177 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_CGROUP=", t); | |||
1178 | ||||
1179 | p = procfs_file_alloca(pid, "mountinfo")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "mountinfo"); } else { _r_ = __builtin_alloca ((sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof(pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof (pid_t) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]) )) + 1 + sizeof("mountinfo")); sprintf((char*) _r_, "/proc/""%" "i""/" "mountinfo", _pid_); } _r_; }); | |||
1180 | if (read_full_file(p, &t, NULL((void*)0)) >=0) | |||
1181 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_MOUNTINFO=", t); | |||
1182 | ||||
1183 | if (get_process_cwd(pid, &t) >= 0) | |||
1184 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CWD=", t); | |||
1185 | ||||
1186 | if (get_process_root(pid, &t) >= 0) { | |||
1187 | bool_Bool proc_self_root_is_slash; | |||
1188 | ||||
1189 | proc_self_root_is_slash = strcmp(t, "/") == 0; | |||
1190 | ||||
1191 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_ROOT=", t); | |||
1192 | ||||
1193 | /* If the process' root is "/", then there is a chance it has | |||
1194 | * mounted own root and hence being containerized. */ | |||
1195 | if (proc_self_root_is_slash
| |||
1196 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_CONTAINER_CMDLINE=", t); | |||
1197 | } | |||
1198 | ||||
1199 | if (get_process_environ(pid, &t) >= 0) | |||
1200 | set_iovec_field_free(iovec, n_iovec, "COREDUMP_ENVIRON=", t); | |||
1201 | ||||
1202 | t = strjoin("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000")strjoin_real(("COREDUMP_TIMESTAMP="), context[CONTEXT_TIMESTAMP ], "000000", ((void*)0)); | |||
1203 | if (t) | |||
1204 | iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t)(struct iovec) { .iov_base = ((char*) t), .iov_len = (strlen( t)) }; | |||
1205 | ||||
1206 | if (safe_atoi(context[CONTEXT_SIGNAL], &signo) >= 0 && SIGNAL_VALID(signo)) | |||
1207 | set_iovec_string_field(iovec, n_iovec, "COREDUMP_SIGNAL_NAME=SIG", signal_to_string(signo)); | |||
1208 | ||||
1209 | return 0; /* we successfully acquired all metadata */ | |||
1210 | } | |||
1211 | ||||
1212 | static int process_kernel(int argc, char* argv[]) { | |||
1213 | ||||
1214 | char* context[_CONTEXT_MAX] = {}; | |||
1215 | struct iovec iovec[29 + SUBMIT_COREDUMP_FIELDS4]; | |||
1216 | size_t i, n_iovec, n_to_free = 0; | |||
1217 | int r; | |||
1218 | ||||
1219 | log_debug("Processing coredump received from the kernel...")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1219, __func__, "Processing coredump received from the kernel..." ) : -abs(_e); }); | |||
1220 | ||||
1221 | if (argc < CONTEXT_COMM + 1) { | |||
1222 | log_error("Not enough arguments passed by the kernel (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1222, __func__, "Not enough arguments passed by the kernel (%i, expected %i)." , argc - 1, CONTEXT_COMM + 1 - 1) : -abs(_e); }); | |||
1223 | return -EINVAL22; | |||
1224 | } | |||
1225 | ||||
1226 | context[CONTEXT_PID] = argv[1 + CONTEXT_PID]; | |||
1227 | context[CONTEXT_UID] = argv[1 + CONTEXT_UID]; | |||
1228 | context[CONTEXT_GID] = argv[1 + CONTEXT_GID]; | |||
1229 | context[CONTEXT_SIGNAL] = argv[1 + CONTEXT_SIGNAL]; | |||
1230 | context[CONTEXT_TIMESTAMP] = argv[1 + CONTEXT_TIMESTAMP]; | |||
1231 | context[CONTEXT_RLIMIT] = argv[1 + CONTEXT_RLIMIT]; | |||
1232 | context[CONTEXT_HOSTNAME] = argv[1 + CONTEXT_HOSTNAME]; | |||
1233 | ||||
1234 | r = gather_pid_metadata(context, argv + 1 + CONTEXT_COMM, iovec, &n_to_free); | |||
1235 | if (r < 0) | |||
1236 | goto finish; | |||
1237 | ||||
1238 | n_iovec = n_to_free; | |||
1239 | ||||
1240 | iovec[n_iovec++] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR)(struct iovec) { .iov_base = ((char*) "MESSAGE_ID=" "fc" "2e" "22" "bc" "6e" "e6" "47" "b6" "b9" "07" "29" "ab" "34" "a2" "50" "b1"), .iov_len = (strlen("MESSAGE_ID=" "fc" "2e" "22" "bc" "6e" "e6" "47" "b6" "b9" "07" "29" "ab" "34" "a2" "50" "b1")) }; | |||
1241 | ||||
1242 | assert_cc(2 == LOG_CRIT)GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_37 { char x[(2 == 2) ? 0 : -1]; }; GCC diagnostic pop ; | |||
1243 | iovec[n_iovec++] = IOVEC_MAKE_STRING("PRIORITY=2")(struct iovec) { .iov_base = ((char*) "PRIORITY=2"), .iov_len = (strlen("PRIORITY=2")) }; | |||
1244 | ||||
1245 | assert(n_iovec <= ELEMENTSOF(iovec))do { if ((__builtin_expect(!!(!(n_iovec <= __extension__ ( __builtin_choose_expr( !__builtin_types_compatible_p(typeof(iovec ), typeof(&*(iovec))), sizeof(iovec)/sizeof((iovec)[0]), ( (void)0))))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ( "n_iovec <= ELEMENTSOF(iovec)"), "../src/coredump/coredump.c" , 1245, __PRETTY_FUNCTION__); } while (0); | |||
1246 | ||||
1247 | if (is_journald_crash((const char**) context) || is_pid1_crash((const char**) context)) | |||
1248 | r = submit_coredump((const char**) context, | |||
1249 | iovec, ELEMENTSOF(iovec)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(iovec), typeof(&*(iovec))), sizeof(iovec)/sizeof( (iovec)[0]), ((void)0))), n_iovec, | |||
1250 | STDIN_FILENO0); | |||
1251 | else | |||
1252 | r = send_iovec(iovec, n_iovec, STDIN_FILENO0); | |||
1253 | ||||
1254 | finish: | |||
1255 | for (i = 0; i < n_to_free; i++) | |||
1256 | free(iovec[i].iov_base); | |||
1257 | ||||
1258 | /* Those fields are allocated by gather_pid_metadata */ | |||
1259 | free(context[CONTEXT_COMM]); | |||
1260 | free(context[CONTEXT_EXE]); | |||
1261 | free(context[CONTEXT_UNIT]); | |||
1262 | ||||
1263 | return r; | |||
1264 | } | |||
1265 | ||||
1266 | static int process_backtrace(int argc, char *argv[]) { | |||
1267 | char *context[_CONTEXT_MAX] = {}; | |||
1268 | _cleanup_free___attribute__((cleanup(freep))) char *message = NULL((void*)0); | |||
1269 | _cleanup_free___attribute__((cleanup(freep))) struct iovec *iovec = NULL((void*)0); | |||
1270 | size_t n_iovec, n_allocated, n_to_free = 0, i; | |||
1271 | int r; | |||
1272 | JournalImporter importer = { | |||
1273 | .fd = STDIN_FILENO0, | |||
1274 | }; | |||
1275 | ||||
1276 | log_debug("Processing backtrace on stdin...")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1276, __func__, "Processing backtrace on stdin..." ) : -abs(_e); }); | |||
1277 | ||||
1278 | if (argc < CONTEXT_COMM + 1) { | |||
1279 | log_error("Not enough arguments passed (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1279, __func__, "Not enough arguments passed (%i, expected %i)." , argc - 1, CONTEXT_COMM + 1 - 1) : -abs(_e); }); | |||
1280 | return -EINVAL22; | |||
1281 | } | |||
1282 | ||||
1283 | context[CONTEXT_PID] = argv[2 + CONTEXT_PID]; | |||
1284 | context[CONTEXT_UID] = argv[2 + CONTEXT_UID]; | |||
1285 | context[CONTEXT_GID] = argv[2 + CONTEXT_GID]; | |||
1286 | context[CONTEXT_SIGNAL] = argv[2 + CONTEXT_SIGNAL]; | |||
1287 | context[CONTEXT_TIMESTAMP] = argv[2 + CONTEXT_TIMESTAMP]; | |||
1288 | context[CONTEXT_RLIMIT] = argv[2 + CONTEXT_RLIMIT]; | |||
1289 | context[CONTEXT_HOSTNAME] = argv[2 + CONTEXT_HOSTNAME]; | |||
1290 | ||||
1291 | n_allocated = 34 + COREDUMP_STORAGE_EXTERNAL; | |||
1292 | /* 26 metadata, 2 static, +unknown input, 4 storage, rounded up */ | |||
1293 | iovec = new(struct iovec, n_allocated)((struct iovec*) malloc_multiply(sizeof(struct iovec), (n_allocated ))); | |||
1294 | if (!iovec) | |||
1295 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1295, __func__); | |||
1296 | ||||
1297 | r = gather_pid_metadata(context, argv + 2 + CONTEXT_COMM, iovec, &n_to_free); | |||
1298 | if (r < 0) | |||
1299 | goto finish; | |||
1300 | if (r > 0) { | |||
1301 | /* This was a special crash, and has already been processed. */ | |||
1302 | r = 0; | |||
1303 | goto finish; | |||
1304 | } | |||
1305 | n_iovec = n_to_free; | |||
1306 | ||||
1307 | for (;;) { | |||
1308 | r = journal_importer_process_data(&importer); | |||
1309 | if (r < 0) { | |||
1310 | log_error_errno(r, "Failed to parse journal entry on stdin: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1310, __func__, "Failed to parse journal entry on stdin: %m" ) : -abs(_e); }); | |||
1311 | goto finish; | |||
1312 | } | |||
1313 | if (r == 1 || /* complete entry */ | |||
1314 | journal_importer_eof(&importer)) /* end of data */ | |||
1315 | break; | |||
1316 | } | |||
1317 | ||||
1318 | if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec + importer.iovw.count + 2)greedy_realloc((void**) &(iovec), &(n_allocated), (n_iovec + importer.iovw.count + 2), sizeof((iovec)[0]))) | |||
1319 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1319, __func__); | |||
1320 | ||||
1321 | if (journal_importer_eof(&importer)) { | |||
1322 | log_warning("Did not receive a full journal entry on stdin, ignoring message sent by reporter")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1322, __func__, "Did not receive a full journal entry on stdin, ignoring message sent by reporter" ) : -abs(_e); }); | |||
1323 | ||||
1324 | message = strjoin("MESSAGE=Process ", context[CONTEXT_PID],strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ")" " of user ", context[CONTEXT_UID ], " failed with ", context[CONTEXT_SIGNAL], ((void*)0)) | |||
1325 | " (", context[CONTEXT_COMM], ")"strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ")" " of user ", context[CONTEXT_UID ], " failed with ", context[CONTEXT_SIGNAL], ((void*)0)) | |||
1326 | " of user ", context[CONTEXT_UID],strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ")" " of user ", context[CONTEXT_UID ], " failed with ", context[CONTEXT_SIGNAL], ((void*)0)) | |||
1327 | " failed with ", context[CONTEXT_SIGNAL])strjoin_real(("MESSAGE=Process "), context[CONTEXT_PID], " (" , context[CONTEXT_COMM], ")" " of user ", context[CONTEXT_UID ], " failed with ", context[CONTEXT_SIGNAL], ((void*)0)); | |||
1328 | if (!message) { | |||
1329 | r = log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/coredump/coredump.c" , 1329, __func__); | |||
1330 | goto finish; | |||
1331 | } | |||
1332 | iovec[n_iovec++] = IOVEC_MAKE_STRING(message)(struct iovec) { .iov_base = ((char*) message), .iov_len = (strlen (message)) }; | |||
1333 | } else { | |||
1334 | for (i = 0; i < importer.iovw.count; i++) | |||
1335 | iovec[n_iovec++] = importer.iovw.iovec[i]; | |||
1336 | } | |||
1337 | ||||
1338 | iovec[n_iovec++] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR)(struct iovec) { .iov_base = ((char*) "MESSAGE_ID=" "1f" "4e" "0a" "44" "a8" "86" "49" "93" "9a" "ae" "a3" "4f" "c6" "da" "8c" "95"), .iov_len = (strlen("MESSAGE_ID=" "1f" "4e" "0a" "44" "a8" "86" "49" "93" "9a" "ae" "a3" "4f" "c6" "da" "8c" "95")) }; | |||
1339 | assert_cc(2 == LOG_CRIT)GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_38 { char x[(2 == 2) ? 0 : -1]; }; GCC diagnostic pop ; | |||
1340 | iovec[n_iovec++] = IOVEC_MAKE_STRING("PRIORITY=2")(struct iovec) { .iov_base = ((char*) "PRIORITY=2"), .iov_len = (strlen("PRIORITY=2")) }; | |||
1341 | ||||
1342 | assert(n_iovec <= n_allocated)do { if ((__builtin_expect(!!(!(n_iovec <= n_allocated)),0 ))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("n_iovec <= n_allocated" ), "../src/coredump/coredump.c", 1342, __PRETTY_FUNCTION__); } while (0); | |||
1343 | ||||
1344 | r = sd_journal_sendv(iovec, n_iovec)sd_journal_sendv_with_location("CODE_FILE=" "../src/coredump/coredump.c" , "CODE_LINE=" "1344", __func__, iovec, n_iovec); | |||
1345 | if (r < 0) | |||
1346 | log_error_errno(r, "Failed to log backtrace: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1346, __func__, "Failed to log backtrace: %m" ) : -abs(_e); }); | |||
1347 | ||||
1348 | finish: | |||
1349 | for (i = 0; i < n_to_free; i++) | |||
1350 | free(iovec[i].iov_base); | |||
1351 | ||||
1352 | /* Those fields are allocated by gather_pid_metadata */ | |||
1353 | free(context[CONTEXT_COMM]); | |||
1354 | free(context[CONTEXT_EXE]); | |||
1355 | free(context[CONTEXT_UNIT]); | |||
1356 | ||||
1357 | return r; | |||
1358 | } | |||
1359 | ||||
1360 | int main(int argc, char *argv[]) { | |||
1361 | int r; | |||
1362 | ||||
1363 | /* First, log to a safe place, since we don't know what crashed and it might | |||
1364 | * be journald which we'd rather not log to then. */ | |||
1365 | ||||
1366 | log_set_target(LOG_TARGET_KMSG); | |||
1367 | log_open(); | |||
1368 | ||||
1369 | /* Make sure we never enter a loop */ | |||
1370 | (void) prctl(PR_SET_DUMPABLE4, 0); | |||
1371 | ||||
1372 | /* Ignore all parse errors */ | |||
1373 | (void) parse_config(); | |||
1374 | ||||
1375 | log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1375, __func__, "Selected storage '%s'." , coredump_storage_to_string(arg_storage)) : -abs(_e); }); | |||
1376 | log_debug("Selected compression %s.", yes_no(arg_compress))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1376, __func__, "Selected compression %s." , yes_no(arg_compress)) : -abs(_e); }); | |||
1377 | ||||
1378 | r = sd_listen_fds(false0); | |||
1379 | if (r < 0) { | |||
1380 | log_error_errno(r, "Failed to determine number of file descriptor: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1380, __func__, "Failed to determine number of file descriptor: %m" ) : -abs(_e); }); | |||
1381 | goto finish; | |||
1382 | } | |||
1383 | ||||
1384 | /* If we got an fd passed, we are running in coredumpd mode. Otherwise we | |||
1385 | * are invoked from the kernel as coredump handler. */ | |||
1386 | if (r == 0) { | |||
1387 | if (streq_ptr(argv[1], "--backtrace")) | |||
1388 | r = process_backtrace(argc, argv); | |||
1389 | else | |||
1390 | r = process_kernel(argc, argv); | |||
1391 | } else if (r == 1) | |||
1392 | r = process_socket(SD_LISTEN_FDS_START3); | |||
1393 | else { | |||
1394 | log_error("Received unexpected number of file descriptors.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/coredump/coredump.c", 1394, __func__, "Received unexpected number of file descriptors." ) : -abs(_e); }); | |||
1395 | r = -EINVAL22; | |||
1396 | } | |||
1397 | ||||
1398 | finish: | |||
1399 | return r < 0 ? EXIT_FAILURE1 : EXIT_SUCCESS0; | |||
1400 | } |