chiark / gitweb /
journal: hook up coredumping with journal
[elogind.git] / src / journal / coredump.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <unistd.h>
24
25 #include <systemd/sd-journal.h>
26 #include <systemd/sd-login.h>
27
28 #include "log.h"
29 #include "util.h"
30
31 #define COREDUMP_MAX (64*1024)
32
33 enum {
34         ARG_PID = 1,
35         ARG_UID,
36         ARG_GID,
37         ARG_SIGNAL,
38         ARG_TIMESTAMP,
39         ARG_COMM,
40         _ARG_MAX
41 };
42
43 int main(int argc, char* argv[]) {
44         int r, j = 0;
45         char *p = NULL;
46         ssize_t n;
47         pid_t pid;
48         struct iovec iovec[14];
49         char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
50                 *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
51                 *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *t;
52
53         log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
54         log_parse_environment();
55         log_open();
56
57         if (argc != _ARG_MAX) {
58                 log_error("Invalid number of arguments passed from kernel.");
59                 r = -EINVAL;
60                 goto finish;
61         }
62
63         r = parse_pid(argv[ARG_PID], &pid);
64         if (r < 0) {
65                 log_error("Failed to parse PID.");
66                 r = -EINVAL;
67                 goto finish;
68         }
69
70         p = malloc(9 + COREDUMP_MAX);
71         if (!p) {
72                 log_error("Out of memory");
73                 r = -ENOMEM;
74                 goto finish;
75         }
76
77         memcpy(p, "COREDUMP=", 9);
78
79         n = loop_read(STDIN_FILENO, p + 9, COREDUMP_MAX, false);
80         if (n < 0) {
81                 log_error("Failed to read core dump data: %s", strerror(-n));
82                 r = (int) n;
83                 goto finish;
84         }
85
86         zero(iovec);
87         iovec[j].iov_base = p;
88         iovec[j].iov_len = 9 + n;
89         j++;
90
91         core_pid = strappend("COREDUMP_PID=", argv[ARG_PID]);
92         if (core_pid)
93                 IOVEC_SET_STRING(iovec[j++], core_pid);
94
95         core_uid = strappend("COREDUMP_UID=", argv[ARG_UID]);
96         if (core_uid)
97                 IOVEC_SET_STRING(iovec[j++], core_uid);
98
99         core_gid = strappend("COREDUMP_GID=", argv[ARG_GID]);
100         if (core_gid)
101                 IOVEC_SET_STRING(iovec[j++], core_gid);
102
103         core_signal = strappend("COREDUMP_SIGNAL=", argv[ARG_SIGNAL]);
104         if (core_signal)
105                 IOVEC_SET_STRING(iovec[j++], core_signal);
106
107         core_comm = strappend("COREDUMP_COMM=", argv[ARG_COMM]);
108         if (core_comm)
109                 IOVEC_SET_STRING(iovec[j++], core_comm);
110
111         if (sd_pid_get_session(pid, &t) >= 0) {
112                 core_session = strappend("COREDUMP_SESSION=", t);
113                 free(t);
114
115                 if (core_session)
116                         IOVEC_SET_STRING(iovec[j++], core_session);
117         }
118
119         if (sd_pid_get_unit(pid, &t) >= 0) {
120                 core_unit = strappend("COREDUMP_UNIT=", t);
121                 free(t);
122
123                 if (core_unit)
124                         IOVEC_SET_STRING(iovec[j++], core_unit);
125         }
126
127         if (get_process_exe(pid, &t) >= 0) {
128                 core_exe = strappend("COREDUMP_EXE=", t);
129                 free(t);
130
131                 if (core_exe)
132                         IOVEC_SET_STRING(iovec[j++], core_exe);
133         }
134
135         if (get_process_cmdline(pid, LINE_MAX, false, &t) >= 0) {
136                 core_cmdline = strappend("COREDUMP_CMDLINE=", t);
137                 free(t);
138
139                 if (core_cmdline)
140                         IOVEC_SET_STRING(iovec[j++], core_cmdline);
141         }
142
143         core_timestamp = join("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL);
144         if (core_timestamp)
145                 IOVEC_SET_STRING(iovec[j++], core_timestamp);
146
147         IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
148         IOVEC_SET_STRING(iovec[j++], "PRIORITY=2");
149
150         core_message = join("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL);
151         if (core_message)
152                 IOVEC_SET_STRING(iovec[j++], core_message);
153
154         r = sd_journal_sendv(iovec, j);
155         if (r < 0)
156                 log_error("Failed to send coredump: %s", strerror(-r));
157
158 finish:
159         free(p);
160         free(core_pid);
161         free(core_uid);
162         free(core_gid);
163         free(core_signal);
164         free(core_timestamp);
165         free(core_comm);
166         free(core_exe);
167         free(core_cmdline);
168         free(core_unit);
169         free(core_session);
170         free(core_message);
171
172         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
173 }