]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/coredump/coredumpctl.c
coredumpctl: report user unit properly
[thirdparty/systemd.git] / src / coredump / coredumpctl.c
CommitLineData
5de0409e
ZJS
1/***
2 This file is part of systemd.
3
4 Copyright 2012 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
3f6fd1ba
LP
20#include <fcntl.h>
21#include <getopt.h>
a9cdc94f 22#include <locale.h>
5de0409e
ZJS
23#include <stdio.h>
24#include <string.h>
ada45c78 25#include <unistd.h>
5de0409e 26
2cf4172a 27#include "sd-journal.h"
3f6fd1ba 28
b5efdb8a 29#include "alloc-util.h"
3f6fd1ba 30#include "compress.h"
3ffd4af2 31#include "fd-util.h"
0d39fa9c 32#include "fileio.h"
992e8f22 33#include "fs-util.h"
3f6fd1ba 34#include "journal-internal.h"
5de0409e 35#include "log.h"
763c7aa2 36#include "macro.h"
3f6fd1ba 37#include "pager.h"
6bedfcbb 38#include "parse-util.h"
3f6fd1ba 39#include "path-util.h"
0b452006 40#include "process-util.h"
3f6fd1ba
LP
41#include "set.h"
42#include "sigbus.h"
24882e06 43#include "signal-util.h"
07630cea 44#include "string-util.h"
3f6fd1ba 45#include "terminal-util.h"
b1d4f8e1 46#include "user-util.h"
6bedfcbb 47#include "util.h"
5de0409e
ZJS
48
49static enum {
50 ACTION_NONE,
e15758cc 51 ACTION_INFO,
5de0409e
ZJS
52 ACTION_LIST,
53 ACTION_DUMP,
ada45c78 54 ACTION_GDB,
5de0409e 55} arg_action = ACTION_LIST;
e15758cc 56static const char* arg_field = NULL;
b73e9a02 57static const char *arg_directory = NULL;
ea4b98e6 58static bool arg_no_pager = false;
9a340880 59static int arg_no_legend = false;
0c51aada 60static int arg_one = false;
3774cf57 61static FILE* arg_output = NULL;
5de0409e
ZJS
62
63static Set *new_matches(void) {
64 Set *set;
65 char *tmp;
66 int r;
67
d5099efc 68 set = set_new(NULL);
5de0409e
ZJS
69 if (!set) {
70 log_oom();
71 return NULL;
72 }
73
74 tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
75 if (!tmp) {
76 log_oom();
4a207bb2 77 set_free(set);
5de0409e
ZJS
78 return NULL;
79 }
80
ef42202a 81 r = set_consume(set, tmp);
5de0409e 82 if (r < 0) {
da927ba9 83 log_error_errno(r, "failed to add to set: %m");
4a207bb2 84 set_free(set);
5de0409e
ZJS
85 return NULL;
86 }
87
88 return set;
89}
90
5de0409e 91static int add_match(Set *set, const char *match) {
7fd1b19b 92 _cleanup_free_ char *p = NULL;
0f474365
LP
93 char *pattern = NULL;
94 const char* prefix;
95 pid_t pid;
96 int r;
5de0409e
ZJS
97
98 if (strchr(match, '='))
99 prefix = "";
100 else if (strchr(match, '/')) {
0f474365
LP
101 r = path_make_absolute_cwd(match, &p);
102 if (r < 0)
5de0409e 103 goto fail;
5de0409e
ZJS
104 match = p;
105 prefix = "COREDUMP_EXE=";
0f474365 106 } else if (parse_pid(match, &pid) >= 0)
5de0409e
ZJS
107 prefix = "COREDUMP_PID=";
108 else
109 prefix = "COREDUMP_COMM=";
110
111 pattern = strjoin(prefix, match, NULL);
0f474365
LP
112 if (!pattern) {
113 r = -ENOMEM;
5de0409e 114 goto fail;
0f474365 115 }
5de0409e 116
ef42202a 117 log_debug("Adding pattern: %s", pattern);
f7a5bb28 118 r = set_consume(set, pattern);
0f474365 119 if (r < 0)
5de0409e 120 goto fail;
5de0409e
ZJS
121
122 return 0;
123fail:
23bbb0de 124 return log_error_errno(r, "Failed to add match: %m");
5de0409e
ZJS
125}
126
601185b4
ZJS
127static void help(void) {
128 printf("%s [OPTIONS...]\n\n"
129 "List or retrieve coredumps from the journal.\n\n"
130 "Flags:\n"
131 " -h --help Show this help\n"
132 " --version Print version string\n"
133 " --no-pager Do not pipe output into a pager\n"
134 " --no-legend Do not print the column headers.\n"
135 " -1 Show information about most recent entry only\n"
136 " -F --field=FIELD List all values a certain field takes\n"
137 " -o --output=FILE Write output to FILE\n\n"
b73e9a02 138 " -D --directory=DIR Use journal files from directory\n\n"
601185b4
ZJS
139
140 "Commands:\n"
141 " list [MATCHES...] List available coredumps (default)\n"
142 " info [MATCHES...] Show detailed information about one or more coredumps\n"
143 " dump [MATCHES...] Print first matching coredump to stdout\n"
144 " gdb [MATCHES...] Start gdb for the first matching coredump\n"
145 , program_invocation_short_name);
146}
147
763c7aa2 148static int parse_argv(int argc, char *argv[], Set *matches) {
5de0409e
ZJS
149 enum {
150 ARG_VERSION = 0x100,
151 ARG_NO_PAGER,
9a340880 152 ARG_NO_LEGEND,
5de0409e
ZJS
153 };
154
155 int r, c;
156
157 static const struct option options[] = {
57ce4bd4
ZJS
158 { "help", no_argument, NULL, 'h' },
159 { "version" , no_argument, NULL, ARG_VERSION },
160 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
9a340880 161 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
57ce4bd4 162 { "output", required_argument, NULL, 'o' },
4f76ae1b 163 { "field", required_argument, NULL, 'F' },
b73e9a02 164 { "directory", required_argument, NULL, 'D' },
eb9da376 165 {}
5de0409e
ZJS
166 };
167
168 assert(argc >= 0);
169 assert(argv);
170
b73e9a02 171 while ((c = getopt_long(argc, argv, "ho:F:1D:", options, NULL)) >= 0)
5de0409e 172 switch(c) {
eb9da376 173
5de0409e 174 case 'h':
5de0409e 175 arg_action = ACTION_NONE;
601185b4
ZJS
176 help();
177 return 0;
5de0409e
ZJS
178
179 case ARG_VERSION:
eb9da376 180 arg_action = ACTION_NONE;
3f6fd1ba 181 return version();
5de0409e
ZJS
182
183 case ARG_NO_PAGER:
184 arg_no_pager = true;
185 break;
186
9a340880
ZJS
187 case ARG_NO_LEGEND:
188 arg_no_legend = true;
189 break;
190
5de0409e 191 case 'o':
3774cf57 192 if (arg_output) {
5de0409e
ZJS
193 log_error("cannot set output more than once");
194 return -EINVAL;
195 }
196
3774cf57
LP
197 arg_output = fopen(optarg, "we");
198 if (!arg_output)
4a62c710 199 return log_error_errno(errno, "writing to '%s': %m", optarg);
5de0409e
ZJS
200
201 break;
57ce4bd4 202
4f76ae1b 203 case 'F':
a276ae74 204 if (arg_field) {
4f76ae1b
ZJS
205 log_error("cannot use --field/-F more than once");
206 return -EINVAL;
207 }
a276ae74 208 arg_field = optarg;
4f76ae1b
ZJS
209 break;
210
0c51aada
LP
211 case '1':
212 arg_one = true;
213 break;
214
b73e9a02
SW
215 case 'D':
216 arg_directory = optarg;
217 break;
218
57ce4bd4
ZJS
219 case '?':
220 return -EINVAL;
221
5de0409e 222 default:
eb9da376 223 assert_not_reached("Unhandled option");
5de0409e
ZJS
224 }
225
226 if (optind < argc) {
227 const char *cmd = argv[optind++];
f168c273 228 if (streq(cmd, "list"))
5de0409e
ZJS
229 arg_action = ACTION_LIST;
230 else if (streq(cmd, "dump"))
231 arg_action = ACTION_DUMP;
ada45c78
LP
232 else if (streq(cmd, "gdb"))
233 arg_action = ACTION_GDB;
e15758cc
LP
234 else if (streq(cmd, "info"))
235 arg_action = ACTION_INFO;
5de0409e
ZJS
236 else {
237 log_error("Unknown action '%s'", cmd);
238 return -EINVAL;
239 }
240 }
241
a276ae74 242 if (arg_field && arg_action != ACTION_LIST) {
4f76ae1b
ZJS
243 log_error("Option --field/-F only makes sense with list");
244 return -EINVAL;
245 }
246
5de0409e
ZJS
247 while (optind < argc) {
248 r = add_match(matches, argv[optind]);
249 if (r != 0)
250 return r;
251 optind++;
252 }
253
254 return 0;
255}
256
8bc8ab83
LP
257static int retrieve(const void *data,
258 size_t len,
259 const char *name,
a276ae74 260 char **var) {
5de0409e 261
4f76ae1b 262 size_t ident;
a276ae74 263 char *v;
8bc8ab83 264
4f76ae1b 265 ident = strlen(name) + 1; /* name + "=" */
8bc8ab83 266
4f76ae1b 267 if (len < ident)
8bc8ab83 268 return 0;
5de0409e 269
4f76ae1b 270 if (memcmp(data, name, ident - 1) != 0)
8bc8ab83
LP
271 return 0;
272
4f76ae1b 273 if (((const char*) data)[ident - 1] != '=')
8bc8ab83 274 return 0;
5de0409e 275
a276ae74
LP
276 v = strndup((const char*)data + ident, len - ident);
277 if (!v)
5de0409e
ZJS
278 return log_oom();
279
a276ae74
LP
280 free(*var);
281 *var = v;
282
5de0409e
ZJS
283 return 0;
284}
285
4f76ae1b 286static void print_field(FILE* file, sd_journal *j) {
a276ae74 287 _cleanup_free_ char *value = NULL;
4f76ae1b
ZJS
288 const void *d;
289 size_t l;
290
e15758cc
LP
291 assert(file);
292 assert(j);
293
a276ae74 294 assert(arg_field);
4f76ae1b
ZJS
295
296 SD_JOURNAL_FOREACH_DATA(j, d, l)
a276ae74
LP
297 retrieve(d, l, arg_field, &value);
298
4f76ae1b
ZJS
299 if (value)
300 fprintf(file, "%s\n", value);
301}
302
e15758cc 303static int print_list(FILE* file, sd_journal *j, int had_legend) {
a276ae74 304 _cleanup_free_ char
5de0409e 305 *pid = NULL, *uid = NULL, *gid = NULL,
9fe13294
ZJS
306 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
307 *filename = NULL;
8bc8ab83
LP
308 const void *d;
309 size_t l;
684341b0
LP
310 usec_t t;
311 char buf[FORMAT_TIMESTAMP_MAX];
312 int r;
9fe13294 313 bool present;
8bc8ab83 314
e15758cc
LP
315 assert(file);
316 assert(j);
317
8bc8ab83 318 SD_JOURNAL_FOREACH_DATA(j, d, l) {
8bc8ab83
LP
319 retrieve(d, l, "COREDUMP_PID", &pid);
320 retrieve(d, l, "COREDUMP_UID", &uid);
321 retrieve(d, l, "COREDUMP_GID", &gid);
322 retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
323 retrieve(d, l, "COREDUMP_EXE", &exe);
a276ae74
LP
324 retrieve(d, l, "COREDUMP_COMM", &comm);
325 retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
9fe13294 326 retrieve(d, l, "COREDUMP_FILENAME", &filename);
8bc8ab83 327 }
5de0409e 328
9fe13294 329 if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) {
684341b0
LP
330 log_warning("Empty coredump log entry");
331 return -EINVAL;
332 }
333
334 r = sd_journal_get_realtime_usec(j, &t);
23bbb0de
MS
335 if (r < 0)
336 return log_error_errno(r, "Failed to get realtime timestamp: %m");
5de0409e 337
684341b0 338 format_timestamp(buf, sizeof(buf), t);
9fe13294 339 present = filename && access(filename, F_OK) == 0;
684341b0 340
9a340880 341 if (!had_legend && !arg_no_legend)
9fe13294 342 fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
c3f84106 343 FORMAT_TIMESTAMP_WIDTH, "TIME",
5de0409e
ZJS
344 6, "PID",
345 5, "UID",
346 5, "GID",
684341b0 347 3, "SIG",
9fe13294 348 1, "PRESENT",
684341b0 349 "EXE");
5de0409e 350
9fe13294 351 fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
c3f84106 352 FORMAT_TIMESTAMP_WIDTH, buf,
a276ae74
LP
353 6, strna(pid),
354 5, strna(uid),
355 5, strna(gid),
356 3, strna(sgnl),
9fe13294 357 1, present ? "*" : "",
a276ae74 358 strna(exe ?: (comm ?: cmdline)));
684341b0
LP
359
360 return 0;
5de0409e
ZJS
361}
362
e15758cc
LP
363static int print_info(FILE *file, sd_journal *j, bool need_space) {
364 _cleanup_free_ char
365 *pid = NULL, *uid = NULL, *gid = NULL,
366 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
367 *unit = NULL, *user_unit = NULL, *session = NULL,
a035f819 368 *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
9fe13294
ZJS
369 *slice = NULL, *cgroup = NULL, *owner_uid = NULL,
370 *message = NULL, *timestamp = NULL, *filename = NULL;
e15758cc
LP
371 const void *d;
372 size_t l;
4b8cbe9a 373 int r;
e15758cc
LP
374
375 assert(file);
376 assert(j);
377
378 SD_JOURNAL_FOREACH_DATA(j, d, l) {
379 retrieve(d, l, "COREDUMP_PID", &pid);
380 retrieve(d, l, "COREDUMP_UID", &uid);
381 retrieve(d, l, "COREDUMP_GID", &gid);
382 retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
383 retrieve(d, l, "COREDUMP_EXE", &exe);
384 retrieve(d, l, "COREDUMP_COMM", &comm);
385 retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
386 retrieve(d, l, "COREDUMP_UNIT", &unit);
387 retrieve(d, l, "COREDUMP_USER_UNIT", &user_unit);
388 retrieve(d, l, "COREDUMP_SESSION", &session);
a035f819
LP
389 retrieve(d, l, "COREDUMP_OWNER_UID", &owner_uid);
390 retrieve(d, l, "COREDUMP_SLICE", &slice);
391 retrieve(d, l, "COREDUMP_CGROUP", &cgroup);
4b8cbe9a 392 retrieve(d, l, "COREDUMP_TIMESTAMP", &timestamp);
9fe13294 393 retrieve(d, l, "COREDUMP_FILENAME", &filename);
e15758cc
LP
394 retrieve(d, l, "_BOOT_ID", &boot_id);
395 retrieve(d, l, "_MACHINE_ID", &machine_id);
396 retrieve(d, l, "_HOSTNAME", &hostname);
8d4e028f 397 retrieve(d, l, "MESSAGE", &message);
e15758cc
LP
398 }
399
400 if (need_space)
401 fputs("\n", file);
402
81cef14f
LP
403 if (comm)
404 fprintf(file,
405 " PID: %s%s%s (%s)\n",
1fc464f6 406 ansi_highlight(), strna(pid), ansi_normal(), comm);
81cef14f
LP
407 else
408 fprintf(file,
409 " PID: %s%s%s\n",
1fc464f6 410 ansi_highlight(), strna(pid), ansi_normal());
a035f819
LP
411
412 if (uid) {
413 uid_t n;
414
415 if (parse_uid(uid, &n) >= 0) {
416 _cleanup_free_ char *u = NULL;
417
418 u = uid_to_name(n);
419 fprintf(file,
420 " UID: %s (%s)\n",
421 uid, u);
422 } else {
423 fprintf(file,
424 " UID: %s\n",
425 uid);
426 }
427 }
428
429 if (gid) {
430 gid_t n;
431
432 if (parse_gid(gid, &n) >= 0) {
433 _cleanup_free_ char *g = NULL;
434
435 g = gid_to_name(n);
436 fprintf(file,
437 " GID: %s (%s)\n",
438 gid, g);
439 } else {
440 fprintf(file,
441 " GID: %s\n",
442 gid);
443 }
444 }
e15758cc
LP
445
446 if (sgnl) {
447 int sig;
448
449 if (safe_atoi(sgnl, &sig) >= 0)
450 fprintf(file, " Signal: %s (%s)\n", sgnl, signal_to_string(sig));
451 else
452 fprintf(file, " Signal: %s\n", sgnl);
453 }
454
4b8cbe9a
LP
455 if (timestamp) {
456 usec_t u;
457
458 r = safe_atou64(timestamp, &u);
459 if (r >= 0) {
460 char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX];
461
462 fprintf(file,
463 " Timestamp: %s (%s)\n",
464 format_timestamp(absolute, sizeof(absolute), u),
465 format_timestamp_relative(relative, sizeof(relative), u));
466
467 } else
468 fprintf(file, " Timestamp: %s\n", timestamp);
469 }
470
e15758cc
LP
471 if (cmdline)
472 fprintf(file, " Command Line: %s\n", cmdline);
81cef14f 473 if (exe)
1fc464f6 474 fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal());
a035f819
LP
475 if (cgroup)
476 fprintf(file, " Control Group: %s\n", cgroup);
e15758cc
LP
477 if (unit)
478 fprintf(file, " Unit: %s\n", unit);
479 if (user_unit)
554ed50f 480 fprintf(file, " User Unit: %s\n", user_unit);
a035f819
LP
481 if (slice)
482 fprintf(file, " Slice: %s\n", slice);
e15758cc
LP
483 if (session)
484 fprintf(file, " Session: %s\n", session);
a035f819
LP
485 if (owner_uid) {
486 uid_t n;
487
488 if (parse_uid(owner_uid, &n) >= 0) {
489 _cleanup_free_ char *u = NULL;
490
491 u = uid_to_name(n);
492 fprintf(file,
493 " Owner UID: %s (%s)\n",
494 owner_uid, u);
495 } else {
496 fprintf(file,
497 " Owner UID: %s\n",
498 owner_uid);
499 }
500 }
e15758cc
LP
501 if (boot_id)
502 fprintf(file, " Boot ID: %s\n", boot_id);
503 if (machine_id)
504 fprintf(file, " Machine ID: %s\n", machine_id);
505 if (hostname)
506 fprintf(file, " Hostname: %s\n", hostname);
507
9fe13294
ZJS
508 if (filename && access(filename, F_OK) == 0)
509 fprintf(file, " Coredump: %s\n", filename);
e15758cc 510
8d4e028f
LP
511 if (message) {
512 _cleanup_free_ char *m = NULL;
513
514 m = strreplace(message, "\n", "\n ");
515
516 fprintf(file, " Message: %s\n", strstrip(m ?: message));
517 }
518
e15758cc
LP
519 return 0;
520}
521
ada45c78 522static int focus(sd_journal *j) {
5de0409e
ZJS
523 int r;
524
5de0409e
ZJS
525 r = sd_journal_seek_tail(j);
526 if (r == 0)
527 r = sd_journal_previous(j);
23bbb0de
MS
528 if (r < 0)
529 return log_error_errno(r, "Failed to search journal: %m");
8bc8ab83 530 if (r == 0) {
0c51aada 531 log_error("No match found.");
8bc8ab83
LP
532 return -ESRCH;
533 }
ada45c78
LP
534 return r;
535}
5de0409e 536
0c51aada
LP
537static void print_entry(sd_journal *j, unsigned n_found) {
538 assert(j);
539
540 if (arg_action == ACTION_INFO)
541 print_info(stdout, j, n_found);
542 else if (arg_field)
543 print_field(stdout, j);
544 else
545 print_list(stdout, j, n_found);
546}
547
548static int dump_list(sd_journal *j) {
549 unsigned n_found = 0;
550 int r;
551
552 assert(j);
553
554 /* The coredumps are likely to compressed, and for just
555 * listing them we don't need to decompress them, so let's
556 * pick a fairly low data threshold here */
557 sd_journal_set_data_threshold(j, 4096);
558
559 if (arg_one) {
560 r = focus(j);
561 if (r < 0)
562 return r;
563
564 print_entry(j, 0);
565 } else {
566 SD_JOURNAL_FOREACH(j)
567 print_entry(j, n_found++);
568
569 if (!arg_field && n_found <= 0) {
570 log_notice("No coredumps found.");
571 return -ESRCH;
572 }
573 }
574
575 return 0;
576}
577
9fe13294
ZJS
578static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
579 const char *data;
580 _cleanup_free_ char *filename = NULL;
581 size_t len;
ada45c78 582 int r;
fc6cec86
ZJS
583 _cleanup_close_ int fdt = -1;
584 char *temp = NULL;
ada45c78 585
9fe13294
ZJS
586 assert((fd >= 0) != !!path);
587 assert(!!path == !!unlink_temp);
ada45c78 588
fc6cec86 589 /* Look for a coredump on disk first. */
9fe13294
ZJS
590 r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
591 if (r < 0 && r != -ENOENT)
fc6cec86 592 return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m");
9fe13294
ZJS
593 else if (r == 0)
594 retrieve(data, len, "COREDUMP_FILENAME", &filename);
93b73b06 595
954d3a51
ZJS
596 if (filename) {
597 if (access(filename, R_OK) < 0)
598 return log_error_errno(errno, "File \"%s\" is not readable: %m", filename);
5de0409e 599
954d3a51 600 if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
d0c8806d
TA
601 *path = filename;
602 filename = NULL;
a276ae74 603
954d3a51
ZJS
604 return 0;
605 }
fc6cec86 606 }
a276ae74 607
fc6cec86
ZJS
608 if (fd < 0) {
609 const char *vt;
992e8f22 610
fc6cec86 611 /* Create a temporary file to write the uncompressed core to. */
992e8f22 612
fc6cec86
ZJS
613 r = var_tmp_dir(&vt);
614 if (r < 0)
615 return log_error_errno(r, "Failed to acquire temporary directory path: %m");
9fe13294 616
fc6cec86
ZJS
617 temp = strjoin(vt, "/coredump-XXXXXX", NULL);
618 if (!temp)
619 return log_oom();
9fe13294 620
fc6cec86
ZJS
621 fdt = mkostemp_safe(temp);
622 if (fdt < 0)
623 return log_error_errno(fdt, "Failed to create temporary file: %m");
624 log_debug("Created temporary file %s", temp);
9fe13294 625
fc6cec86
ZJS
626 fd = fdt;
627 }
628
629 if (filename) {
d89c8fdf 630#if defined(HAVE_XZ) || defined(HAVE_LZ4)
fc6cec86 631 _cleanup_close_ int fdf;
9fe13294 632
fc6cec86
ZJS
633 fdf = open(filename, O_RDONLY | O_CLOEXEC);
634 if (fdf < 0) {
635 r = log_error_errno(errno, "Failed to open %s: %m", filename);
2fb8159f 636 goto error;
fc6cec86
ZJS
637 }
638
639 r = decompress_stream(filename, fdf, fd, -1);
640 if (r < 0) {
641 log_error_errno(r, "Failed to decompress %s: %m", filename);
9fe13294
ZJS
642 goto error;
643 }
fc6cec86
ZJS
644#else
645 log_error("Cannot decompress file. Compiled without compression support.");
646 r = -EOPNOTSUPP;
647 goto error;
648#endif
649 } else {
650 ssize_t sz;
a276ae74 651
fc6cec86
ZJS
652 r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
653 if (r < 0)
654 return log_error_errno(r,
655 r == -ENOENT ? "Core file was not saved for this entry." :
656 "Failed to retrieve COREDUMP field: %m");
657
658 assert(len >= 9);
659 data += 9;
660 len -= 9;
661
954d3a51 662 sz = write(fd, data, len);
fc6cec86 663 if (sz < 0) {
954d3a51 664 r = log_error_errno(errno, "Failed to write output: %m");
fc6cec86 665 goto error;
a276ae74 666 }
fc6cec86 667 if (sz != (ssize_t) len) {
954d3a51 668 log_error("Short write to output.");
fc6cec86
ZJS
669 r = -EIO;
670 goto error;
671 }
672 }
a276ae74 673
fc6cec86
ZJS
674 if (temp) {
675 *path = temp;
676 *unlink_temp = true;
677 }
678 return 0;
9fe13294
ZJS
679
680error:
fc6cec86
ZJS
681 if (temp) {
682 unlink(temp);
683 log_debug("Removed temporary file %s", temp);
9fe13294 684 }
fc6cec86 685 return r;
9fe13294 686}
a276ae74 687
9fe13294
ZJS
688static int dump_core(sd_journal* j) {
689 int r;
690
691 assert(j);
692
693 r = focus(j);
694 if (r < 0)
ada45c78 695 return r;
ada45c78 696
3774cf57 697 print_info(arg_output ? stdout : stderr, j, false);
9fe13294 698
3774cf57 699 if (on_tty() && !arg_output) {
9fe13294
ZJS
700 log_error("Refusing to dump core to tty.");
701 return -ENOTTY;
702 }
703
3774cf57 704 r = save_core(j, arg_output ? fileno(arg_output) : STDOUT_FILENO, NULL, NULL);
23bbb0de
MS
705 if (r < 0)
706 return log_error_errno(r, "Coredump retrieval failed: %m");
5de0409e
ZJS
707
708 r = sd_journal_previous(j);
cfeead6c 709 if (r > 0)
9f6445e3 710 log_warning("More than one entry matches, ignoring rest.");
5de0409e
ZJS
711
712 return 0;
713}
714
ada45c78 715static int run_gdb(sd_journal *j) {
de8f6e54 716 _cleanup_free_ char *exe = NULL, *path = NULL;
9fe13294
ZJS
717 bool unlink_path = false;
718 const char *data;
a276ae74 719 siginfo_t st;
ada45c78 720 size_t len;
ada45c78 721 pid_t pid;
ada45c78 722 int r;
ada45c78
LP
723
724 assert(j);
725
726 r = focus(j);
727 if (r < 0)
728 return r;
729
e15758cc
LP
730 print_info(stdout, j, false);
731 fputs("\n", stdout);
ada45c78
LP
732
733 r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
23bbb0de
MS
734 if (r < 0)
735 return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
ada45c78 736
9fe13294
ZJS
737 assert(len > strlen("COREDUMP_EXE="));
738 data += strlen("COREDUMP_EXE=");
739 len -= strlen("COREDUMP_EXE=");
ada45c78
LP
740
741 exe = strndup(data, len);
742 if (!exe)
743 return log_oom();
744
745 if (endswith(exe, " (deleted)")) {
746 log_error("Binary already deleted.");
747 return -ENOENT;
748 }
749
a276ae74
LP
750 if (!path_is_absolute(exe)) {
751 log_error("Binary is not an absolute path.");
752 return -ENOENT;
753 }
754
9fe13294 755 r = save_core(j, -1, &path, &unlink_path);
23bbb0de
MS
756 if (r < 0)
757 return log_error_errno(r, "Failed to retrieve core: %m");
ada45c78
LP
758
759 pid = fork();
760 if (pid < 0) {
76ef789d 761 r = log_error_errno(errno, "Failed to fork(): %m");
ada45c78
LP
762 goto finish;
763 }
764 if (pid == 0) {
ce30c8dc
LP
765 (void) reset_all_signal_handlers();
766 (void) reset_signal_mask();
767
ada45c78 768 execlp("gdb", "gdb", exe, path, NULL);
a276ae74 769
56f64d95 770 log_error_errno(errno, "Failed to invoke gdb: %m");
ada45c78
LP
771 _exit(1);
772 }
773
774 r = wait_for_terminate(pid, &st);
775 if (r < 0) {
709f6e46 776 log_error_errno(r, "Failed to wait for gdb: %m");
ada45c78
LP
777 goto finish;
778 }
779
780 r = st.si_code == CLD_EXITED ? st.si_status : 255;
781
782finish:
9fe13294
ZJS
783 if (unlink_path) {
784 log_debug("Removed temporary file %s", path);
785 unlink(path);
786 }
a276ae74 787
ada45c78
LP
788 return r;
789}
790
5de0409e 791int main(int argc, char *argv[]) {
4afd3348 792 _cleanup_(sd_journal_closep) sd_journal*j = NULL;
5de0409e
ZJS
793 const char* match;
794 Iterator it;
795 int r = 0;
7fd1b19b 796 _cleanup_set_free_free_ Set *matches = NULL;
5de0409e 797
a9cdc94f 798 setlocale(LC_ALL, "");
5de0409e
ZJS
799 log_parse_environment();
800 log_open();
801
802 matches = new_matches();
2fb7a5ce
ZJS
803 if (!matches) {
804 r = -ENOMEM;
5de0409e 805 goto end;
2fb7a5ce 806 }
5de0409e 807
763c7aa2 808 r = parse_argv(argc, argv, matches);
2fb7a5ce 809 if (r < 0)
5de0409e
ZJS
810 goto end;
811
812 if (arg_action == ACTION_NONE)
813 goto end;
814
2cf4172a
LP
815 sigbus_install();
816
b73e9a02
SW
817 if (arg_directory) {
818 r = sd_journal_open_directory(&j, arg_directory, 0);
819 if (r < 0) {
820 log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory);
821 goto end;
822 }
823 } else {
824 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
825 if (r < 0) {
826 log_error_errno(r, "Failed to open journal: %m");
827 goto end;
828 }
5de0409e
ZJS
829 }
830
9fe13294
ZJS
831 /* We want full data, nothing truncated. */
832 sd_journal_set_data_threshold(j, 0);
833
5de0409e 834 SET_FOREACH(match, matches, it) {
5de0409e
ZJS
835 r = sd_journal_add_match(j, match, strlen(match));
836 if (r != 0) {
c33b3297
MS
837 log_error_errno(r, "Failed to add match '%s': %m",
838 match);
5de0409e
ZJS
839 goto end;
840 }
841 }
842
553d2243 843 if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) {
6c17bf04
ZJS
844 _cleanup_free_ char *filter;
845
846 filter = journal_make_match_string(j);
847 log_debug("Journal filter: %s", filter);
848 }
849
5de0409e 850 switch(arg_action) {
ada45c78 851
5de0409e 852 case ACTION_LIST:
e15758cc 853 case ACTION_INFO:
ea4b98e6 854 pager_open(arg_no_pager, false);
5de0409e
ZJS
855 r = dump_list(j);
856 break;
ada45c78 857
5de0409e
ZJS
858 case ACTION_DUMP:
859 r = dump_core(j);
860 break;
ada45c78
LP
861
862 case ACTION_GDB:
863 r = run_gdb(j);
864 break;
865
866 default:
5de0409e
ZJS
867 assert_not_reached("Shouldn't be here");
868 }
869
870end:
5de0409e
ZJS
871 pager_close();
872
3774cf57
LP
873 if (arg_output)
874 fclose(arg_output);
5de0409e 875
ada45c78 876 return r >= 0 ? r : EXIT_FAILURE;
5de0409e 877}