]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5de0409e | 2 | |
3f6fd1ba LP |
3 | #include <fcntl.h> |
4 | #include <getopt.h> | |
a9cdc94f | 5 | #include <locale.h> |
5de0409e | 6 | #include <stdio.h> |
ada45c78 | 7 | #include <unistd.h> |
5de0409e | 8 | |
012f2b7d | 9 | #include "sd-bus.h" |
2cf4172a | 10 | #include "sd-journal.h" |
2b044526 | 11 | #include "sd-messages.h" |
3f6fd1ba | 12 | |
b5efdb8a | 13 | #include "alloc-util.h" |
012f2b7d ZJS |
14 | #include "bus-error.h" |
15 | #include "bus-util.h" | |
3f6fd1ba | 16 | #include "compress.h" |
1abaf488 | 17 | #include "def.h" |
3ffd4af2 | 18 | #include "fd-util.h" |
992e8f22 | 19 | #include "fs-util.h" |
aeb56450 | 20 | #include "glob-util.h" |
3f6fd1ba | 21 | #include "journal-internal.h" |
b9aaa7f4 | 22 | #include "journal-util.h" |
5de0409e | 23 | #include "log.h" |
763c7aa2 | 24 | #include "macro.h" |
c118b577 | 25 | #include "main-func.h" |
3f6fd1ba | 26 | #include "pager.h" |
6bedfcbb | 27 | #include "parse-util.h" |
3f6fd1ba | 28 | #include "path-util.h" |
294bf0c3 | 29 | #include "pretty-print.h" |
0b452006 | 30 | #include "process-util.h" |
1abaf488 | 31 | #include "rlimit-util.h" |
3f6fd1ba | 32 | #include "sigbus.h" |
24882e06 | 33 | #include "signal-util.h" |
07630cea | 34 | #include "string-util.h" |
5ab9ed07 | 35 | #include "strv.h" |
3f6fd1ba | 36 | #include "terminal-util.h" |
e4de7287 | 37 | #include "tmpfile-util.h" |
b1d4f8e1 | 38 | #include "user-util.h" |
6bedfcbb | 39 | #include "util.h" |
5ce97d33 | 40 | #include "verbs.h" |
5de0409e | 41 | |
501551e8 LP |
42 | #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC) |
43 | ||
32485d09 | 44 | static usec_t arg_since = USEC_INFINITY, arg_until = USEC_INFINITY; |
e15758cc | 45 | static const char* arg_field = NULL; |
c5896b6a | 46 | static const char *arg_debugger = NULL; |
b73e9a02 | 47 | static const char *arg_directory = NULL; |
aeb56450 | 48 | static char **arg_file = NULL; |
0221d68a | 49 | static PagerFlags arg_pager_flags = 0; |
9a340880 | 50 | static int arg_no_legend = false; |
0c51aada | 51 | static int arg_one = false; |
158ecef5 | 52 | static const char* arg_output = NULL; |
df65f77b | 53 | static bool arg_reverse = false; |
b9aaa7f4 | 54 | static bool arg_quiet = false; |
5de0409e | 55 | |
aeb56450 FS |
56 | STATIC_DESTRUCTOR_REGISTER(arg_file, strv_freep); |
57 | ||
5ab9ed07 | 58 | static int add_match(sd_journal *j, const char *match) { |
7fd1b19b | 59 | _cleanup_free_ char *p = NULL; |
43bfe750 | 60 | const char* prefix, *pattern; |
0f474365 LP |
61 | pid_t pid; |
62 | int r; | |
5de0409e ZJS |
63 | |
64 | if (strchr(match, '=')) | |
65 | prefix = ""; | |
66 | else if (strchr(match, '/')) { | |
0f474365 LP |
67 | r = path_make_absolute_cwd(match, &p); |
68 | if (r < 0) | |
5ab9ed07 ZJS |
69 | return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match); |
70 | ||
5de0409e ZJS |
71 | match = p; |
72 | prefix = "COREDUMP_EXE="; | |
0f474365 | 73 | } else if (parse_pid(match, &pid) >= 0) |
5de0409e ZJS |
74 | prefix = "COREDUMP_PID="; |
75 | else | |
76 | prefix = "COREDUMP_COMM="; | |
77 | ||
e8fb0238 | 78 | pattern = strjoina(prefix, match); |
5ab9ed07 ZJS |
79 | log_debug("Adding match: %s", pattern); |
80 | r = sd_journal_add_match(j, pattern, 0); | |
81 | if (r < 0) | |
82 | return log_error_errno(r, "Failed to add match \"%s\": %m", match); | |
43bfe750 | 83 | |
5ab9ed07 ZJS |
84 | return 0; |
85 | } | |
86 | ||
5ce97d33 | 87 | static int add_matches(sd_journal *j, char **matches) { |
5ab9ed07 ZJS |
88 | char **match; |
89 | int r; | |
5de0409e | 90 | |
2b044526 | 91 | r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, 0); |
0f474365 | 92 | if (r < 0) |
2b044526 | 93 | return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); |
5ab9ed07 | 94 | |
a7581ff9 ZJS |
95 | r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, 0); |
96 | if (r < 0) | |
97 | return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR); | |
98 | ||
5ce97d33 | 99 | STRV_FOREACH(match, matches) { |
5ab9ed07 ZJS |
100 | r = add_match(j, *match); |
101 | if (r < 0) | |
102 | return r; | |
103 | } | |
5de0409e ZJS |
104 | |
105 | return 0; | |
5de0409e ZJS |
106 | } |
107 | ||
5ce97d33 YW |
108 | static int acquire_journal(sd_journal **ret, char **matches) { |
109 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
110 | int r; | |
111 | ||
112 | assert(ret); | |
113 | ||
114 | if (arg_directory) { | |
115 | r = sd_journal_open_directory(&j, arg_directory, 0); | |
116 | if (r < 0) | |
117 | return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory); | |
aeb56450 FS |
118 | } else if (arg_file) { |
119 | r = sd_journal_open_files(&j, (const char**)arg_file, 0); | |
120 | if (r < 0) | |
121 | return log_error_errno(r, "Failed to open journal files: %m"); | |
5ce97d33 YW |
122 | } else { |
123 | r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); | |
124 | if (r < 0) | |
125 | return log_error_errno(r, "Failed to open journal: %m"); | |
126 | } | |
127 | ||
128 | r = journal_access_check_and_warn(j, arg_quiet, true); | |
129 | if (r < 0) | |
130 | return r; | |
131 | ||
132 | r = add_matches(j, matches); | |
133 | if (r < 0) | |
134 | return r; | |
135 | ||
136 | if (DEBUG_LOGGING) { | |
137 | _cleanup_free_ char *filter; | |
138 | ||
139 | filter = journal_make_match_string(j); | |
140 | log_debug("Journal filter: %s", filter); | |
141 | } | |
142 | ||
1cc6c93a | 143 | *ret = TAKE_PTR(j); |
5ce97d33 YW |
144 | |
145 | return 0; | |
146 | } | |
147 | ||
148 | static int help(void) { | |
37ec0fdd LP |
149 | _cleanup_free_ char *link = NULL; |
150 | int r; | |
151 | ||
152 | r = terminal_urlify_man("coredumpctl", "1", &link); | |
153 | if (r < 0) | |
154 | return log_oom(); | |
155 | ||
353b2baa LP |
156 | printf("%s [OPTIONS...] COMMAND ...\n\n" |
157 | "%sList or retrieve coredumps from the journal.%s\n" | |
e1fac8a6 ZJS |
158 | "\nCommands:\n" |
159 | " list [MATCHES...] List available coredumps (default)\n" | |
160 | " info [MATCHES...] Show detailed information about one or more coredumps\n" | |
161 | " dump [MATCHES...] Print first matching coredump to stdout\n" | |
162 | " debug [MATCHES...] Start a debugger for the first matching coredump\n" | |
353b2baa | 163 | "\nOptions:\n" |
c5896b6a RG |
164 | " -h --help Show this help\n" |
165 | " --version Print version string\n" | |
166 | " --no-pager Do not pipe output into a pager\n" | |
167 | " --no-legend Do not print the column headers\n" | |
168 | " --debugger=DEBUGGER Use the given debugger\n" | |
169 | " -1 Show information about most recent entry only\n" | |
170 | " -S --since=DATE Only print coredumps since the date\n" | |
171 | " -U --until=DATE Only print coredumps until the date\n" | |
172 | " -r --reverse Show the newest entries first\n" | |
173 | " -F --field=FIELD List all values a certain field takes\n" | |
174 | " -o --output=FILE Write output to FILE\n" | |
aeb56450 | 175 | " --file=PATH Use journal file\n" |
c5896b6a RG |
176 | " -D --directory=DIR Use journal files from directory\n\n" |
177 | " -q --quiet Do not show info messages and privilege warning\n" | |
37ec0fdd LP |
178 | "\nSee the %s for details.\n" |
179 | , program_invocation_short_name | |
353b2baa | 180 | , ansi_highlight() |
ce2529b4 | 181 | , ansi_normal() |
37ec0fdd LP |
182 | , link |
183 | ); | |
5ce97d33 YW |
184 | |
185 | return 0; | |
601185b4 ZJS |
186 | } |
187 | ||
5ab9ed07 | 188 | static int parse_argv(int argc, char *argv[]) { |
5de0409e ZJS |
189 | enum { |
190 | ARG_VERSION = 0x100, | |
191 | ARG_NO_PAGER, | |
9a340880 | 192 | ARG_NO_LEGEND, |
c5896b6a | 193 | ARG_DEBUGGER, |
aeb56450 | 194 | ARG_FILE, |
5de0409e ZJS |
195 | }; |
196 | ||
32485d09 | 197 | int c, r; |
5de0409e ZJS |
198 | |
199 | static const struct option options[] = { | |
57ce4bd4 ZJS |
200 | { "help", no_argument, NULL, 'h' }, |
201 | { "version" , no_argument, NULL, ARG_VERSION }, | |
202 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
9a340880 | 203 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, |
c5896b6a | 204 | { "debugger", required_argument, NULL, ARG_DEBUGGER }, |
57ce4bd4 | 205 | { "output", required_argument, NULL, 'o' }, |
4f76ae1b | 206 | { "field", required_argument, NULL, 'F' }, |
aeb56450 | 207 | { "file", required_argument, NULL, ARG_FILE }, |
b73e9a02 | 208 | { "directory", required_argument, NULL, 'D' }, |
df65f77b | 209 | { "reverse", no_argument, NULL, 'r' }, |
32485d09 GS |
210 | { "since", required_argument, NULL, 'S' }, |
211 | { "until", required_argument, NULL, 'U' }, | |
b9aaa7f4 | 212 | { "quiet", no_argument, NULL, 'q' }, |
eb9da376 | 213 | {} |
5de0409e ZJS |
214 | }; |
215 | ||
216 | assert(argc >= 0); | |
217 | assert(argv); | |
218 | ||
b9aaa7f4 | 219 | while ((c = getopt_long(argc, argv, "ho:F:1D:rS:U:q", options, NULL)) >= 0) |
5de0409e ZJS |
220 | switch(c) { |
221 | case 'h': | |
5ce97d33 | 222 | return help(); |
5de0409e ZJS |
223 | |
224 | case ARG_VERSION: | |
3f6fd1ba | 225 | return version(); |
5de0409e ZJS |
226 | |
227 | case ARG_NO_PAGER: | |
0221d68a | 228 | arg_pager_flags |= PAGER_DISABLE; |
5de0409e ZJS |
229 | break; |
230 | ||
9a340880 ZJS |
231 | case ARG_NO_LEGEND: |
232 | arg_no_legend = true; | |
233 | break; | |
234 | ||
c5896b6a RG |
235 | case ARG_DEBUGGER: |
236 | arg_debugger = optarg; | |
237 | break; | |
238 | ||
aeb56450 | 239 | case ARG_FILE: |
544e146b | 240 | r = glob_extend(&arg_file, optarg, GLOB_NOCHECK); |
aeb56450 FS |
241 | if (r < 0) |
242 | return log_error_errno(r, "Failed to add paths: %m"); | |
243 | break; | |
244 | ||
5de0409e | 245 | case 'o': |
baaa35ad ZJS |
246 | if (arg_output) |
247 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
248 | "Cannot set output more than once."); | |
5de0409e | 249 | |
158ecef5 | 250 | arg_output = optarg; |
5de0409e | 251 | break; |
57ce4bd4 | 252 | |
32485d09 GS |
253 | case 'S': |
254 | r = parse_timestamp(optarg, &arg_since); | |
255 | if (r < 0) | |
9d77ca39 | 256 | return log_error_errno(r, "Failed to parse timestamp '%s': %m", optarg); |
32485d09 GS |
257 | break; |
258 | ||
259 | case 'U': | |
260 | r = parse_timestamp(optarg, &arg_until); | |
261 | if (r < 0) | |
9d77ca39 | 262 | return log_error_errno(r, "Failed to parse timestamp '%s': %m", optarg); |
32485d09 GS |
263 | break; |
264 | ||
4f76ae1b | 265 | case 'F': |
baaa35ad ZJS |
266 | if (arg_field) |
267 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
268 | "Cannot use --field/-F more than once."); | |
a276ae74 | 269 | arg_field = optarg; |
4f76ae1b ZJS |
270 | break; |
271 | ||
0c51aada LP |
272 | case '1': |
273 | arg_one = true; | |
274 | break; | |
275 | ||
b73e9a02 SW |
276 | case 'D': |
277 | arg_directory = optarg; | |
278 | break; | |
279 | ||
df65f77b NK |
280 | case 'r': |
281 | arg_reverse = true; | |
282 | break; | |
283 | ||
b9aaa7f4 ZJS |
284 | case 'q': |
285 | arg_quiet = true; | |
286 | break; | |
287 | ||
57ce4bd4 ZJS |
288 | case '?': |
289 | return -EINVAL; | |
290 | ||
5de0409e | 291 | default: |
eb9da376 | 292 | assert_not_reached("Unhandled option"); |
5de0409e ZJS |
293 | } |
294 | ||
32485d09 | 295 | if (arg_since != USEC_INFINITY && arg_until != USEC_INFINITY && |
baaa35ad ZJS |
296 | arg_since > arg_until) |
297 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
298 | "--since= must be before --until=."); | |
32485d09 | 299 | |
5ce97d33 | 300 | return 1; |
5de0409e ZJS |
301 | } |
302 | ||
8bc8ab83 LP |
303 | static int retrieve(const void *data, |
304 | size_t len, | |
305 | const char *name, | |
a276ae74 | 306 | char **var) { |
5de0409e | 307 | |
4f76ae1b | 308 | size_t ident; |
a276ae74 | 309 | char *v; |
8bc8ab83 | 310 | |
4f76ae1b | 311 | ident = strlen(name) + 1; /* name + "=" */ |
8bc8ab83 | 312 | |
4f76ae1b | 313 | if (len < ident) |
8bc8ab83 | 314 | return 0; |
5de0409e | 315 | |
4f76ae1b | 316 | if (memcmp(data, name, ident - 1) != 0) |
8bc8ab83 LP |
317 | return 0; |
318 | ||
4f76ae1b | 319 | if (((const char*) data)[ident - 1] != '=') |
8bc8ab83 | 320 | return 0; |
5de0409e | 321 | |
a276ae74 LP |
322 | v = strndup((const char*)data + ident, len - ident); |
323 | if (!v) | |
5de0409e ZJS |
324 | return log_oom(); |
325 | ||
eba048bb | 326 | free_and_replace(*var, v); |
062b99e8 | 327 | return 1; |
5de0409e ZJS |
328 | } |
329 | ||
062b99e8 | 330 | static int print_field(FILE* file, sd_journal *j) { |
4f76ae1b ZJS |
331 | const void *d; |
332 | size_t l; | |
333 | ||
e15758cc LP |
334 | assert(file); |
335 | assert(j); | |
336 | ||
a276ae74 | 337 | assert(arg_field); |
4f76ae1b | 338 | |
062b99e8 | 339 | /* A (user-specified) field may appear more than once for a given entry. |
5238e957 | 340 | * We will print all of the occurrences. |
062b99e8 ZJS |
341 | * This is different below for fields that systemd-coredump uses, |
342 | * because they cannot meaningfully appear more than once. | |
343 | */ | |
344 | SD_JOURNAL_FOREACH_DATA(j, d, l) { | |
345 | _cleanup_free_ char *value = NULL; | |
346 | int r; | |
347 | ||
348 | r = retrieve(d, l, arg_field, &value); | |
349 | if (r < 0) | |
350 | return r; | |
351 | if (r > 0) | |
352 | fprintf(file, "%s\n", value); | |
353 | } | |
a276ae74 | 354 | |
062b99e8 | 355 | return 0; |
4f76ae1b ZJS |
356 | } |
357 | ||
062b99e8 ZJS |
358 | #define RETRIEVE(d, l, name, arg) \ |
359 | { \ | |
360 | int _r = retrieve(d, l, name, &arg); \ | |
361 | if (_r < 0) \ | |
362 | return _r; \ | |
363 | if (_r > 0) \ | |
364 | continue; \ | |
365 | } | |
366 | ||
e15758cc | 367 | static int print_list(FILE* file, sd_journal *j, int had_legend) { |
a276ae74 | 368 | _cleanup_free_ char |
a7581ff9 | 369 | *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, |
9fe13294 | 370 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, |
cc4419ed | 371 | *filename = NULL, *truncated = NULL, *coredump = NULL; |
8bc8ab83 LP |
372 | const void *d; |
373 | size_t l; | |
684341b0 LP |
374 | usec_t t; |
375 | char buf[FORMAT_TIMESTAMP_MAX]; | |
376 | int r; | |
04de5879 | 377 | const char *present; |
a7581ff9 | 378 | bool normal_coredump; |
8bc8ab83 | 379 | |
e15758cc LP |
380 | assert(file); |
381 | assert(j); | |
382 | ||
8bc8ab83 | 383 | SD_JOURNAL_FOREACH_DATA(j, d, l) { |
a7581ff9 | 384 | RETRIEVE(d, l, "MESSAGE_ID", mid); |
062b99e8 ZJS |
385 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
386 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
387 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
388 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
389 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
390 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
391 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
392 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
cc4419ed | 393 | RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); |
062b99e8 | 394 | RETRIEVE(d, l, "COREDUMP", coredump); |
8bc8ab83 | 395 | } |
5de0409e | 396 | |
9fe13294 | 397 | if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) { |
684341b0 LP |
398 | log_warning("Empty coredump log entry"); |
399 | return -EINVAL; | |
400 | } | |
401 | ||
402 | r = sd_journal_get_realtime_usec(j, &t); | |
23bbb0de MS |
403 | if (r < 0) |
404 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
5de0409e | 405 | |
684341b0 LP |
406 | format_timestamp(buf, sizeof(buf), t); |
407 | ||
9a340880 | 408 | if (!had_legend && !arg_no_legend) |
cc4419ed | 409 | fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n", |
c3f84106 | 410 | FORMAT_TIMESTAMP_WIDTH, "TIME", |
5de0409e ZJS |
411 | 6, "PID", |
412 | 5, "UID", | |
413 | 5, "GID", | |
684341b0 | 414 | 3, "SIG", |
cc4419ed | 415 | 9, "COREFILE", |
684341b0 | 416 | "EXE"); |
5de0409e | 417 | |
a7581ff9 ZJS |
418 | normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); |
419 | ||
04de5879 ZJS |
420 | if (filename) |
421 | if (access(filename, R_OK) == 0) | |
422 | present = "present"; | |
423 | else if (errno == ENOENT) | |
424 | present = "missing"; | |
425 | else | |
426 | present = "error"; | |
427 | else if (coredump) | |
428 | present = "journal"; | |
a7581ff9 | 429 | else if (normal_coredump) |
04de5879 | 430 | present = "none"; |
a7581ff9 ZJS |
431 | else |
432 | present = "-"; | |
04de5879 | 433 | |
32a1575f | 434 | if (STR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0) |
cc4419ed ZJS |
435 | present = "truncated"; |
436 | ||
04de5879 | 437 | fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n", |
c3f84106 | 438 | FORMAT_TIMESTAMP_WIDTH, buf, |
a276ae74 LP |
439 | 6, strna(pid), |
440 | 5, strna(uid), | |
441 | 5, strna(gid), | |
a7581ff9 | 442 | 3, normal_coredump ? strna(sgnl) : "-", |
cc4419ed | 443 | 9, present, |
a276ae74 | 444 | strna(exe ?: (comm ?: cmdline))); |
684341b0 LP |
445 | |
446 | return 0; | |
5de0409e ZJS |
447 | } |
448 | ||
e15758cc LP |
449 | static int print_info(FILE *file, sd_journal *j, bool need_space) { |
450 | _cleanup_free_ char | |
a7581ff9 | 451 | *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, |
e15758cc LP |
452 | *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, |
453 | *unit = NULL, *user_unit = NULL, *session = NULL, | |
a035f819 | 454 | *boot_id = NULL, *machine_id = NULL, *hostname = NULL, |
9fe13294 | 455 | *slice = NULL, *cgroup = NULL, *owner_uid = NULL, |
47f50642 | 456 | *message = NULL, *timestamp = NULL, *filename = NULL, |
cc4419ed | 457 | *truncated = NULL, *coredump = NULL; |
e15758cc LP |
458 | const void *d; |
459 | size_t l; | |
a7581ff9 | 460 | bool normal_coredump; |
4b8cbe9a | 461 | int r; |
e15758cc LP |
462 | |
463 | assert(file); | |
464 | assert(j); | |
465 | ||
466 | SD_JOURNAL_FOREACH_DATA(j, d, l) { | |
a7581ff9 | 467 | RETRIEVE(d, l, "MESSAGE_ID", mid); |
062b99e8 ZJS |
468 | RETRIEVE(d, l, "COREDUMP_PID", pid); |
469 | RETRIEVE(d, l, "COREDUMP_UID", uid); | |
470 | RETRIEVE(d, l, "COREDUMP_GID", gid); | |
471 | RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); | |
472 | RETRIEVE(d, l, "COREDUMP_EXE", exe); | |
473 | RETRIEVE(d, l, "COREDUMP_COMM", comm); | |
474 | RETRIEVE(d, l, "COREDUMP_CMDLINE", cmdline); | |
475 | RETRIEVE(d, l, "COREDUMP_UNIT", unit); | |
476 | RETRIEVE(d, l, "COREDUMP_USER_UNIT", user_unit); | |
477 | RETRIEVE(d, l, "COREDUMP_SESSION", session); | |
478 | RETRIEVE(d, l, "COREDUMP_OWNER_UID", owner_uid); | |
479 | RETRIEVE(d, l, "COREDUMP_SLICE", slice); | |
480 | RETRIEVE(d, l, "COREDUMP_CGROUP", cgroup); | |
481 | RETRIEVE(d, l, "COREDUMP_TIMESTAMP", timestamp); | |
482 | RETRIEVE(d, l, "COREDUMP_FILENAME", filename); | |
cc4419ed | 483 | RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); |
062b99e8 ZJS |
484 | RETRIEVE(d, l, "COREDUMP", coredump); |
485 | RETRIEVE(d, l, "_BOOT_ID", boot_id); | |
486 | RETRIEVE(d, l, "_MACHINE_ID", machine_id); | |
487 | RETRIEVE(d, l, "_HOSTNAME", hostname); | |
488 | RETRIEVE(d, l, "MESSAGE", message); | |
e15758cc LP |
489 | } |
490 | ||
491 | if (need_space) | |
492 | fputs("\n", file); | |
493 | ||
a7581ff9 ZJS |
494 | normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); |
495 | ||
81cef14f LP |
496 | if (comm) |
497 | fprintf(file, | |
498 | " PID: %s%s%s (%s)\n", | |
1fc464f6 | 499 | ansi_highlight(), strna(pid), ansi_normal(), comm); |
81cef14f LP |
500 | else |
501 | fprintf(file, | |
502 | " PID: %s%s%s\n", | |
1fc464f6 | 503 | ansi_highlight(), strna(pid), ansi_normal()); |
a035f819 LP |
504 | |
505 | if (uid) { | |
506 | uid_t n; | |
507 | ||
508 | if (parse_uid(uid, &n) >= 0) { | |
509 | _cleanup_free_ char *u = NULL; | |
510 | ||
511 | u = uid_to_name(n); | |
512 | fprintf(file, | |
513 | " UID: %s (%s)\n", | |
514 | uid, u); | |
515 | } else { | |
516 | fprintf(file, | |
517 | " UID: %s\n", | |
518 | uid); | |
519 | } | |
520 | } | |
521 | ||
522 | if (gid) { | |
523 | gid_t n; | |
524 | ||
525 | if (parse_gid(gid, &n) >= 0) { | |
526 | _cleanup_free_ char *g = NULL; | |
527 | ||
528 | g = gid_to_name(n); | |
529 | fprintf(file, | |
530 | " GID: %s (%s)\n", | |
531 | gid, g); | |
532 | } else { | |
533 | fprintf(file, | |
534 | " GID: %s\n", | |
535 | gid); | |
536 | } | |
537 | } | |
e15758cc LP |
538 | |
539 | if (sgnl) { | |
540 | int sig; | |
a7581ff9 | 541 | const char *name = normal_coredump ? "Signal" : "Reason"; |
e15758cc | 542 | |
a7581ff9 ZJS |
543 | if (normal_coredump && safe_atoi(sgnl, &sig) >= 0) |
544 | fprintf(file, " %s: %s (%s)\n", name, sgnl, signal_to_string(sig)); | |
e15758cc | 545 | else |
a7581ff9 | 546 | fprintf(file, " %s: %s\n", name, sgnl); |
e15758cc LP |
547 | } |
548 | ||
4b8cbe9a LP |
549 | if (timestamp) { |
550 | usec_t u; | |
551 | ||
552 | r = safe_atou64(timestamp, &u); | |
553 | if (r >= 0) { | |
554 | char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX]; | |
555 | ||
556 | fprintf(file, | |
557 | " Timestamp: %s (%s)\n", | |
558 | format_timestamp(absolute, sizeof(absolute), u), | |
559 | format_timestamp_relative(relative, sizeof(relative), u)); | |
560 | ||
561 | } else | |
562 | fprintf(file, " Timestamp: %s\n", timestamp); | |
563 | } | |
564 | ||
e15758cc LP |
565 | if (cmdline) |
566 | fprintf(file, " Command Line: %s\n", cmdline); | |
81cef14f | 567 | if (exe) |
1fc464f6 | 568 | fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal()); |
a035f819 LP |
569 | if (cgroup) |
570 | fprintf(file, " Control Group: %s\n", cgroup); | |
e15758cc LP |
571 | if (unit) |
572 | fprintf(file, " Unit: %s\n", unit); | |
573 | if (user_unit) | |
554ed50f | 574 | fprintf(file, " User Unit: %s\n", user_unit); |
a035f819 LP |
575 | if (slice) |
576 | fprintf(file, " Slice: %s\n", slice); | |
e15758cc LP |
577 | if (session) |
578 | fprintf(file, " Session: %s\n", session); | |
a035f819 LP |
579 | if (owner_uid) { |
580 | uid_t n; | |
581 | ||
582 | if (parse_uid(owner_uid, &n) >= 0) { | |
583 | _cleanup_free_ char *u = NULL; | |
584 | ||
585 | u = uid_to_name(n); | |
586 | fprintf(file, | |
587 | " Owner UID: %s (%s)\n", | |
588 | owner_uid, u); | |
589 | } else { | |
590 | fprintf(file, | |
591 | " Owner UID: %s\n", | |
592 | owner_uid); | |
593 | } | |
594 | } | |
e15758cc LP |
595 | if (boot_id) |
596 | fprintf(file, " Boot ID: %s\n", boot_id); | |
597 | if (machine_id) | |
598 | fprintf(file, " Machine ID: %s\n", machine_id); | |
599 | if (hostname) | |
600 | fprintf(file, " Hostname: %s\n", hostname); | |
601 | ||
cc4419ed | 602 | if (filename) { |
32a1575f LP |
603 | bool inacc, trunc; |
604 | ||
605 | inacc = access(filename, R_OK) < 0; | |
606 | trunc = truncated && parse_boolean(truncated) > 0; | |
cc4419ed ZJS |
607 | |
608 | if (inacc || trunc) | |
609 | fprintf(file, " Storage: %s%s (%s%s%s)%s\n", | |
610 | ansi_highlight_red(), | |
611 | filename, | |
612 | inacc ? "inaccessible" : "", | |
613 | inacc && trunc ? ", " : "", | |
614 | trunc ? "truncated" : "", | |
615 | ansi_normal()); | |
616 | else | |
617 | fprintf(file, " Storage: %s\n", filename); | |
618 | } | |
619 | ||
47f50642 ZJS |
620 | else if (coredump) |
621 | fprintf(file, " Storage: journal\n"); | |
622 | else | |
623 | fprintf(file, " Storage: none\n"); | |
e15758cc | 624 | |
8d4e028f LP |
625 | if (message) { |
626 | _cleanup_free_ char *m = NULL; | |
627 | ||
628 | m = strreplace(message, "\n", "\n "); | |
629 | ||
630 | fprintf(file, " Message: %s\n", strstrip(m ?: message)); | |
631 | } | |
632 | ||
e15758cc LP |
633 | return 0; |
634 | } | |
635 | ||
ada45c78 | 636 | static int focus(sd_journal *j) { |
5de0409e ZJS |
637 | int r; |
638 | ||
5de0409e ZJS |
639 | r = sd_journal_seek_tail(j); |
640 | if (r == 0) | |
641 | r = sd_journal_previous(j); | |
23bbb0de MS |
642 | if (r < 0) |
643 | return log_error_errno(r, "Failed to search journal: %m"); | |
baaa35ad ZJS |
644 | if (r == 0) |
645 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
646 | "No match found."); | |
ada45c78 LP |
647 | return r; |
648 | } | |
5de0409e | 649 | |
5ce97d33 | 650 | static int print_entry(sd_journal *j, unsigned n_found, bool verb_is_info) { |
0c51aada LP |
651 | assert(j); |
652 | ||
5ce97d33 | 653 | if (verb_is_info) |
062b99e8 | 654 | return print_info(stdout, j, n_found); |
0c51aada | 655 | else if (arg_field) |
062b99e8 | 656 | return print_field(stdout, j); |
0c51aada | 657 | else |
062b99e8 | 658 | return print_list(stdout, j, n_found); |
0c51aada LP |
659 | } |
660 | ||
5ce97d33 YW |
661 | static int dump_list(int argc, char **argv, void *userdata) { |
662 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
0c51aada | 663 | unsigned n_found = 0; |
5ce97d33 | 664 | bool verb_is_info; |
0c51aada LP |
665 | int r; |
666 | ||
5ce97d33 YW |
667 | verb_is_info = (argc >= 1 && streq(argv[0], "info")); |
668 | ||
669 | r = acquire_journal(&j, argv + 1); | |
670 | if (r < 0) | |
671 | return r; | |
672 | ||
0221d68a | 673 | (void) pager_open(arg_pager_flags); |
0c51aada LP |
674 | |
675 | /* The coredumps are likely to compressed, and for just | |
676 | * listing them we don't need to decompress them, so let's | |
677 | * pick a fairly low data threshold here */ | |
678 | sd_journal_set_data_threshold(j, 4096); | |
679 | ||
2fafabfd LY |
680 | /* "info" without pattern implies "-1" */ |
681 | if (arg_one || (verb_is_info && argc == 1)) { | |
0c51aada LP |
682 | r = focus(j); |
683 | if (r < 0) | |
684 | return r; | |
685 | ||
5ce97d33 | 686 | return print_entry(j, 0, verb_is_info); |
0c51aada | 687 | } else { |
32485d09 GS |
688 | if (arg_since != USEC_INFINITY && !arg_reverse) |
689 | r = sd_journal_seek_realtime_usec(j, arg_since); | |
690 | else if (arg_until != USEC_INFINITY && arg_reverse) | |
691 | r = sd_journal_seek_realtime_usec(j, arg_until); | |
692 | else if (arg_reverse) | |
693 | r = sd_journal_seek_tail(j); | |
694 | else | |
695 | r = sd_journal_seek_head(j); | |
696 | if (r < 0) | |
697 | return log_error_errno(r, "Failed to seek to date: %m"); | |
698 | ||
699 | for (;;) { | |
700 | if (!arg_reverse) | |
701 | r = sd_journal_next(j); | |
702 | else | |
703 | r = sd_journal_previous(j); | |
704 | ||
705 | if (r < 0) | |
706 | return log_error_errno(r, "Failed to iterate through journal: %m"); | |
707 | ||
708 | if (r == 0) | |
709 | break; | |
710 | ||
711 | if (arg_until != USEC_INFINITY && !arg_reverse) { | |
712 | usec_t usec; | |
713 | ||
714 | r = sd_journal_get_realtime_usec(j, &usec); | |
df65f77b | 715 | if (r < 0) |
32485d09 GS |
716 | return log_error_errno(r, "Failed to determine timestamp: %m"); |
717 | if (usec > arg_until) | |
718 | continue; | |
df65f77b | 719 | } |
32485d09 GS |
720 | |
721 | if (arg_since != USEC_INFINITY && arg_reverse) { | |
722 | usec_t usec; | |
723 | ||
724 | r = sd_journal_get_realtime_usec(j, &usec); | |
df65f77b | 725 | if (r < 0) |
32485d09 GS |
726 | return log_error_errno(r, "Failed to determine timestamp: %m"); |
727 | if (usec < arg_since) | |
728 | continue; | |
df65f77b | 729 | } |
32485d09 | 730 | |
5ce97d33 | 731 | r = print_entry(j, n_found++, verb_is_info); |
32485d09 GS |
732 | if (r < 0) |
733 | return r; | |
062b99e8 | 734 | } |
0c51aada LP |
735 | |
736 | if (!arg_field && n_found <= 0) { | |
b9aaa7f4 ZJS |
737 | if (!arg_quiet) |
738 | log_notice("No coredumps found."); | |
0c51aada LP |
739 | return -ESRCH; |
740 | } | |
741 | } | |
742 | ||
743 | return 0; | |
744 | } | |
745 | ||
bb7c5bad | 746 | static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { |
9fe13294 ZJS |
747 | const char *data; |
748 | _cleanup_free_ char *filename = NULL; | |
749 | size_t len; | |
bb7c5bad | 750 | int r, fd; |
fc6cec86 ZJS |
751 | _cleanup_close_ int fdt = -1; |
752 | char *temp = NULL; | |
ada45c78 | 753 | |
bb7c5bad ZJS |
754 | assert(!(file && path)); /* At most one can be specified */ |
755 | assert(!!path == !!unlink_temp); /* Those must be specified together */ | |
47f50642 | 756 | |
fc6cec86 | 757 | /* Look for a coredump on disk first. */ |
9fe13294 | 758 | r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); |
2d0a880f ZJS |
759 | if (r == 0) { |
760 | r = retrieve(data, len, "COREDUMP_FILENAME", &filename); | |
761 | if (r < 0) | |
762 | return r; | |
763 | assert(r > 0); | |
764 | ||
765 | if (access(filename, R_OK) < 0) | |
766 | return log_error_errno(errno, "File \"%s\" is not readable: %m", filename); | |
767 | ||
ef5924aa | 768 | if (path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { |
2d0a880f ZJS |
769 | *path = TAKE_PTR(filename); |
770 | ||
771 | return 0; | |
772 | } | |
773 | ||
774 | } else { | |
bb7c5bad ZJS |
775 | if (r != -ENOENT) |
776 | return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); | |
777 | /* Check that we can have a COREDUMP field. We still haven't set a high | |
778 | * data threshold, so we'll get a few kilobytes at most. | |
779 | */ | |
780 | ||
781 | r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); | |
782 | if (r == -ENOENT) | |
783 | return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk)."); | |
784 | if (r < 0) | |
785 | return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); | |
786 | } | |
93b73b06 | 787 | |
bb7c5bad | 788 | if (path) { |
fc6cec86 | 789 | const char *vt; |
992e8f22 | 790 | |
fc6cec86 | 791 | /* Create a temporary file to write the uncompressed core to. */ |
992e8f22 | 792 | |
fc6cec86 ZJS |
793 | r = var_tmp_dir(&vt); |
794 | if (r < 0) | |
795 | return log_error_errno(r, "Failed to acquire temporary directory path: %m"); | |
9fe13294 | 796 | |
657ee2d8 | 797 | temp = path_join(vt, "coredump-XXXXXX"); |
fc6cec86 ZJS |
798 | if (!temp) |
799 | return log_oom(); | |
9fe13294 | 800 | |
fc6cec86 ZJS |
801 | fdt = mkostemp_safe(temp); |
802 | if (fdt < 0) | |
803 | return log_error_errno(fdt, "Failed to create temporary file: %m"); | |
804 | log_debug("Created temporary file %s", temp); | |
9fe13294 | 805 | |
fc6cec86 | 806 | fd = fdt; |
bb7c5bad ZJS |
807 | } else { |
808 | /* If neither path or file are specified, we will write to stdout. Let's now check | |
809 | * if stdout is connected to a tty. We checked that the file exists, or that the | |
810 | * core might be stored in the journal. In this second case, if we found the entry, | |
5238e957 | 811 | * in all likelihood we will be able to access the COREDUMP= field. In either case, |
bb7c5bad ZJS |
812 | * we stop before doing any "real" work, i.e. before starting decompression or |
813 | * reading from the file or creating temporary files. | |
814 | */ | |
815 | if (!file) { | |
816 | if (on_tty()) | |
886cf317 ZJS |
817 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), |
818 | "Refusing to dump core to tty" | |
bb7c5bad ZJS |
819 | " (use shell redirection or specify --output)."); |
820 | file = stdout; | |
821 | } | |
822 | ||
823 | fd = fileno(file); | |
fc6cec86 ZJS |
824 | } |
825 | ||
826 | if (filename) { | |
d80b051c | 827 | #if HAVE_COMPRESSION |
fc6cec86 | 828 | _cleanup_close_ int fdf; |
9fe13294 | 829 | |
fc6cec86 ZJS |
830 | fdf = open(filename, O_RDONLY | O_CLOEXEC); |
831 | if (fdf < 0) { | |
832 | r = log_error_errno(errno, "Failed to open %s: %m", filename); | |
2fb8159f | 833 | goto error; |
fc6cec86 ZJS |
834 | } |
835 | ||
836 | r = decompress_stream(filename, fdf, fd, -1); | |
837 | if (r < 0) { | |
838 | log_error_errno(r, "Failed to decompress %s: %m", filename); | |
9fe13294 ZJS |
839 | goto error; |
840 | } | |
fc6cec86 | 841 | #else |
3afe5c00 LP |
842 | r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
843 | "Cannot decompress file. Compiled without compression support."); | |
fc6cec86 ZJS |
844 | goto error; |
845 | #endif | |
846 | } else { | |
847 | ssize_t sz; | |
a276ae74 | 848 | |
bb7c5bad ZJS |
849 | /* We want full data, nothing truncated. */ |
850 | sd_journal_set_data_threshold(j, 0); | |
851 | ||
fc6cec86 ZJS |
852 | r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); |
853 | if (r < 0) | |
bb7c5bad | 854 | return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); |
fc6cec86 ZJS |
855 | |
856 | assert(len >= 9); | |
857 | data += 9; | |
858 | len -= 9; | |
859 | ||
954d3a51 | 860 | sz = write(fd, data, len); |
fc6cec86 | 861 | if (sz < 0) { |
954d3a51 | 862 | r = log_error_errno(errno, "Failed to write output: %m"); |
fc6cec86 | 863 | goto error; |
a276ae74 | 864 | } |
fc6cec86 | 865 | if (sz != (ssize_t) len) { |
954d3a51 | 866 | log_error("Short write to output."); |
fc6cec86 ZJS |
867 | r = -EIO; |
868 | goto error; | |
869 | } | |
870 | } | |
a276ae74 | 871 | |
fc6cec86 ZJS |
872 | if (temp) { |
873 | *path = temp; | |
874 | *unlink_temp = true; | |
875 | } | |
876 | return 0; | |
9fe13294 ZJS |
877 | |
878 | error: | |
fc6cec86 | 879 | if (temp) { |
6990fb6b | 880 | (void) unlink(temp); |
fc6cec86 | 881 | log_debug("Removed temporary file %s", temp); |
9fe13294 | 882 | } |
fc6cec86 | 883 | return r; |
9fe13294 | 884 | } |
a276ae74 | 885 | |
5ce97d33 YW |
886 | static int dump_core(int argc, char **argv, void *userdata) { |
887 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; | |
158ecef5 | 888 | _cleanup_fclose_ FILE *f = NULL; |
9fe13294 ZJS |
889 | int r; |
890 | ||
d7a0f1f4 FS |
891 | if (arg_field) |
892 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
893 | "Option --field/-F only makes sense with list"); | |
5ce97d33 YW |
894 | |
895 | r = acquire_journal(&j, argv + 1); | |
896 | if (r < 0) | |
897 | return r; | |
9fe13294 ZJS |
898 | |
899 | r = focus(j); | |
900 | if (r < 0) | |
ada45c78 | 901 | return r; |
ada45c78 | 902 | |
158ecef5 ZJS |
903 | if (arg_output) { |
904 | f = fopen(arg_output, "we"); | |
905 | if (!f) | |
906 | return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); | |
907 | } | |
908 | ||
909 | print_info(f ? stdout : stderr, j, false); | |
9fe13294 | 910 | |
158ecef5 | 911 | r = save_core(j, f, NULL, NULL); |
23bbb0de | 912 | if (r < 0) |
bb7c5bad | 913 | return r; |
5de0409e ZJS |
914 | |
915 | r = sd_journal_previous(j); | |
b9aaa7f4 ZJS |
916 | if (r > 0 && !arg_quiet) |
917 | log_notice("More than one entry matches, ignoring rest."); | |
5de0409e ZJS |
918 | |
919 | return 0; | |
920 | } | |
921 | ||
c5896b6a | 922 | static int run_debug(int argc, char **argv, void *userdata) { |
5ce97d33 | 923 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
3f0d8b2d | 924 | _cleanup_free_ char *exe = NULL, *path = NULL, *debugger = NULL; |
9fe13294 | 925 | bool unlink_path = false; |
c5896b6a | 926 | const char *data, *fork_name; |
ada45c78 | 927 | size_t len; |
ada45c78 | 928 | pid_t pid; |
ada45c78 | 929 | int r; |
ada45c78 | 930 | |
c5896b6a RG |
931 | if (!arg_debugger) { |
932 | char *env_debugger; | |
933 | ||
934 | env_debugger = getenv("SYSTEMD_DEBUGGER"); | |
935 | if (env_debugger) | |
936 | arg_debugger = env_debugger; | |
937 | else | |
938 | arg_debugger = "gdb"; | |
939 | } | |
940 | ||
3f0d8b2d YW |
941 | debugger = strdup(arg_debugger); |
942 | if (!debugger) | |
943 | return -ENOMEM; | |
944 | ||
d7a0f1f4 FS |
945 | if (arg_field) |
946 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
947 | "Option --field/-F only makes sense with list"); | |
5ce97d33 YW |
948 | |
949 | r = acquire_journal(&j, argv + 1); | |
950 | if (r < 0) | |
951 | return r; | |
ada45c78 LP |
952 | |
953 | r = focus(j); | |
954 | if (r < 0) | |
955 | return r; | |
956 | ||
e15758cc LP |
957 | print_info(stdout, j, false); |
958 | fputs("\n", stdout); | |
ada45c78 LP |
959 | |
960 | r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len); | |
23bbb0de MS |
961 | if (r < 0) |
962 | return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); | |
ada45c78 | 963 | |
fbd0b64f LP |
964 | assert(len > STRLEN("COREDUMP_EXE=")); |
965 | data += STRLEN("COREDUMP_EXE="); | |
966 | len -= STRLEN("COREDUMP_EXE="); | |
ada45c78 LP |
967 | |
968 | exe = strndup(data, len); | |
969 | if (!exe) | |
970 | return log_oom(); | |
971 | ||
d7a0f1f4 FS |
972 | if (endswith(exe, " (deleted)")) |
973 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
974 | "Binary already deleted."); | |
ada45c78 | 975 | |
d7a0f1f4 FS |
976 | if (!path_is_absolute(exe)) |
977 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
978 | "Binary is not an absolute path."); | |
a276ae74 | 979 | |
bb7c5bad | 980 | r = save_core(j, NULL, &path, &unlink_path); |
23bbb0de | 981 | if (r < 0) |
bb7c5bad | 982 | return r; |
ada45c78 | 983 | |
3e7bc89b FB |
984 | /* Don't interfere with gdb and its handling of SIGINT. */ |
985 | (void) ignore_signals(SIGINT, -1); | |
986 | ||
3f0d8b2d | 987 | fork_name = strjoina("(", debugger, ")"); |
c5896b6a | 988 | |
0672e2c6 | 989 | r = safe_fork(fork_name, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
b6e1fff1 | 990 | if (r < 0) |
ada45c78 | 991 | goto finish; |
4c253ed1 | 992 | if (r == 0) { |
3f0d8b2d | 993 | execlp(debugger, debugger, exe, "-c", path, NULL); |
0b1f3c76 | 994 | log_open(); |
3f0d8b2d | 995 | log_error_errno(errno, "Failed to invoke %s: %m", debugger); |
a45d7127 | 996 | _exit(EXIT_FAILURE); |
ada45c78 LP |
997 | } |
998 | ||
3f0d8b2d | 999 | r = wait_for_terminate_and_check(debugger, pid, WAIT_LOG_ABNORMAL); |
ada45c78 LP |
1000 | |
1001 | finish: | |
3e7bc89b FB |
1002 | (void) default_signals(SIGINT, -1); |
1003 | ||
9fe13294 ZJS |
1004 | if (unlink_path) { |
1005 | log_debug("Removed temporary file %s", path); | |
6990fb6b | 1006 | (void) unlink(path); |
9fe13294 | 1007 | } |
a276ae74 | 1008 | |
ada45c78 LP |
1009 | return r; |
1010 | } | |
1011 | ||
012f2b7d | 1012 | static int check_units_active(void) { |
7add4883 | 1013 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
012f2b7d ZJS |
1014 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
1015 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1016 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1017 | int c = 0, r; | |
7bbf2d84 | 1018 | const char *id, *state, *substate; |
012f2b7d | 1019 | |
b9aaa7f4 ZJS |
1020 | if (arg_quiet) |
1021 | return false; | |
1022 | ||
012f2b7d ZJS |
1023 | r = sd_bus_default_system(&bus); |
1024 | if (r < 0) | |
1025 | return log_error_errno(r, "Failed to acquire bus: %m"); | |
1026 | ||
1027 | r = sd_bus_message_new_method_call( | |
1028 | bus, | |
1029 | &m, | |
1030 | "org.freedesktop.systemd1", | |
1031 | "/org/freedesktop/systemd1", | |
1032 | "org.freedesktop.systemd1.Manager", | |
1033 | "ListUnitsByPatterns"); | |
1034 | if (r < 0) | |
1035 | return bus_log_create_error(r); | |
1036 | ||
1037 | r = sd_bus_message_append_strv(m, NULL); | |
1038 | if (r < 0) | |
1039 | return bus_log_create_error(r); | |
1040 | ||
1041 | r = sd_bus_message_append_strv(m, STRV_MAKE("systemd-coredump@*.service")); | |
1042 | if (r < 0) | |
1043 | return bus_log_create_error(r); | |
1044 | ||
501551e8 | 1045 | r = sd_bus_call(bus, m, SHORT_BUS_CALL_TIMEOUT_USEC, &error, &reply); |
012f2b7d ZJS |
1046 | if (r < 0) |
1047 | return log_error_errno(r, "Failed to check if any systemd-coredump@.service units are running: %s", | |
1048 | bus_error_message(&error, r)); | |
1049 | ||
1050 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); | |
1051 | if (r < 0) | |
1052 | return bus_log_parse_error(r); | |
1053 | ||
1054 | while ((r = sd_bus_message_read( | |
1055 | reply, "(ssssssouso)", | |
7bbf2d84 ZJS |
1056 | &id, NULL, NULL, &state, &substate, |
1057 | NULL, NULL, NULL, NULL, NULL)) > 0) { | |
eb5877a0 | 1058 | bool found = !STR_IN_SET(state, "inactive", "dead", "failed"); |
7bbf2d84 ZJS |
1059 | log_debug("Unit %s is %s/%s, %scounting it.", id, state, substate, found ? "" : "not "); |
1060 | c += found; | |
1061 | } | |
012f2b7d ZJS |
1062 | if (r < 0) |
1063 | return bus_log_parse_error(r); | |
1064 | ||
1065 | r = sd_bus_message_exit_container(reply); | |
1066 | if (r < 0) | |
1067 | return bus_log_parse_error(r); | |
1068 | ||
1069 | return c; | |
1070 | } | |
1071 | ||
5ce97d33 YW |
1072 | static int coredumpctl_main(int argc, char *argv[]) { |
1073 | ||
1074 | static const Verb verbs[] = { | |
c5896b6a RG |
1075 | { "list", VERB_ANY, VERB_ANY, VERB_DEFAULT, dump_list }, |
1076 | { "info", VERB_ANY, VERB_ANY, 0, dump_list }, | |
1077 | { "dump", VERB_ANY, VERB_ANY, 0, dump_core }, | |
1078 | { "debug", VERB_ANY, VERB_ANY, 0, run_debug }, | |
1079 | { "gdb", VERB_ANY, VERB_ANY, 0, run_debug }, | |
5ce97d33 YW |
1080 | {} |
1081 | }; | |
1082 | ||
1083 | return dispatch_verb(argc, argv, verbs, NULL); | |
1084 | } | |
1085 | ||
c118b577 | 1086 | static int run(int argc, char *argv[]) { |
5ce97d33 | 1087 | int r, units_active; |
5de0409e | 1088 | |
a9cdc94f | 1089 | setlocale(LC_ALL, ""); |
41d1f469 | 1090 | log_setup_cli(); |
5de0409e | 1091 | |
1abaf488 LP |
1092 | /* The journal merging logic potentially needs a lot of fds. */ |
1093 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
1094 | ||
5ab9ed07 | 1095 | r = parse_argv(argc, argv); |
5ce97d33 | 1096 | if (r <= 0) |
c118b577 | 1097 | return r; |
5de0409e | 1098 | |
2cf4172a LP |
1099 | sigbus_install(); |
1100 | ||
012f2b7d ZJS |
1101 | units_active = check_units_active(); /* error is treated the same as 0 */ |
1102 | ||
5ce97d33 | 1103 | r = coredumpctl_main(argc, argv); |
5de0409e | 1104 | |
012f2b7d ZJS |
1105 | if (units_active > 0) |
1106 | printf("%s-- Notice: %d systemd-coredump@.service %s, output may be incomplete.%s\n", | |
1107 | ansi_highlight_red(), | |
1108 | units_active, units_active == 1 ? "unit is running" : "units are running", | |
1109 | ansi_normal()); | |
aeb56450 | 1110 | |
c118b577 | 1111 | return r; |
5de0409e | 1112 | } |
c118b577 ZJS |
1113 | |
1114 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |