]>
Commit | Line | Data |
---|---|---|
f5e04665 LP |
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 | ||
de0229ca | 31 | #define COREDUMP_MAX (24*1024*1024) |
f5e04665 LP |
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; | |
fee80f69 LP |
48 | uid_t uid; |
49 | gid_t gid; | |
f5e04665 LP |
50 | struct iovec iovec[14]; |
51 | char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL, | |
52 | *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL, | |
53 | *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *t; | |
54 | ||
55 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |
56 | log_parse_environment(); | |
57 | log_open(); | |
58 | ||
59 | if (argc != _ARG_MAX) { | |
60 | log_error("Invalid number of arguments passed from kernel."); | |
61 | r = -EINVAL; | |
62 | goto finish; | |
63 | } | |
64 | ||
65 | r = parse_pid(argv[ARG_PID], &pid); | |
66 | if (r < 0) { | |
67 | log_error("Failed to parse PID."); | |
f5e04665 LP |
68 | goto finish; |
69 | } | |
70 | ||
fee80f69 LP |
71 | r = parse_uid(argv[ARG_UID], &uid); |
72 | if (r < 0) { | |
73 | log_error("Failed to parse UID."); | |
f5e04665 LP |
74 | goto finish; |
75 | } | |
76 | ||
fee80f69 LP |
77 | r = parse_gid(argv[ARG_GID], &gid); |
78 | if (r < 0) { | |
79 | log_error("Failed to parse GID."); | |
f5e04665 LP |
80 | goto finish; |
81 | } | |
82 | ||
f5e04665 LP |
83 | core_pid = strappend("COREDUMP_PID=", argv[ARG_PID]); |
84 | if (core_pid) | |
85 | IOVEC_SET_STRING(iovec[j++], core_pid); | |
86 | ||
87 | core_uid = strappend("COREDUMP_UID=", argv[ARG_UID]); | |
88 | if (core_uid) | |
89 | IOVEC_SET_STRING(iovec[j++], core_uid); | |
90 | ||
91 | core_gid = strappend("COREDUMP_GID=", argv[ARG_GID]); | |
92 | if (core_gid) | |
93 | IOVEC_SET_STRING(iovec[j++], core_gid); | |
94 | ||
95 | core_signal = strappend("COREDUMP_SIGNAL=", argv[ARG_SIGNAL]); | |
96 | if (core_signal) | |
97 | IOVEC_SET_STRING(iovec[j++], core_signal); | |
98 | ||
99 | core_comm = strappend("COREDUMP_COMM=", argv[ARG_COMM]); | |
100 | if (core_comm) | |
101 | IOVEC_SET_STRING(iovec[j++], core_comm); | |
102 | ||
103 | if (sd_pid_get_session(pid, &t) >= 0) { | |
104 | core_session = strappend("COREDUMP_SESSION=", t); | |
105 | free(t); | |
106 | ||
107 | if (core_session) | |
108 | IOVEC_SET_STRING(iovec[j++], core_session); | |
109 | } | |
110 | ||
111 | if (sd_pid_get_unit(pid, &t) >= 0) { | |
112 | core_unit = strappend("COREDUMP_UNIT=", t); | |
113 | free(t); | |
114 | ||
115 | if (core_unit) | |
116 | IOVEC_SET_STRING(iovec[j++], core_unit); | |
117 | } | |
118 | ||
119 | if (get_process_exe(pid, &t) >= 0) { | |
120 | core_exe = strappend("COREDUMP_EXE=", t); | |
121 | free(t); | |
122 | ||
123 | if (core_exe) | |
124 | IOVEC_SET_STRING(iovec[j++], core_exe); | |
125 | } | |
126 | ||
127 | if (get_process_cmdline(pid, LINE_MAX, false, &t) >= 0) { | |
128 | core_cmdline = strappend("COREDUMP_CMDLINE=", t); | |
129 | free(t); | |
130 | ||
131 | if (core_cmdline) | |
132 | IOVEC_SET_STRING(iovec[j++], core_cmdline); | |
133 | } | |
134 | ||
135 | core_timestamp = join("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL); | |
136 | if (core_timestamp) | |
137 | IOVEC_SET_STRING(iovec[j++], core_timestamp); | |
138 | ||
139 | IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1"); | |
140 | IOVEC_SET_STRING(iovec[j++], "PRIORITY=2"); | |
141 | ||
142 | core_message = join("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL); | |
143 | if (core_message) | |
144 | IOVEC_SET_STRING(iovec[j++], core_message); | |
145 | ||
fee80f69 LP |
146 | /* Now, let's drop privileges to become the user who owns the |
147 | * segfaulted process and allocate the coredump memory under | |
148 | * his uid. This also ensures that the credentials journald | |
149 | * will see are the ones of the coredumping user, thus making | |
150 | * sure the user himself gets access to the core dump. */ | |
151 | ||
152 | if (setresgid(gid, gid, gid) < 0 || | |
153 | setresuid(uid, uid, uid) < 0) { | |
154 | log_error("Failed to drop privileges: %m"); | |
155 | r = -errno; | |
156 | goto finish; | |
157 | } | |
158 | ||
159 | p = malloc(9 + COREDUMP_MAX); | |
160 | if (!p) { | |
161 | log_error("Out of memory"); | |
162 | r = -ENOMEM; | |
163 | goto finish; | |
164 | } | |
165 | ||
166 | memcpy(p, "COREDUMP=", 9); | |
167 | ||
168 | n = loop_read(STDIN_FILENO, p + 9, COREDUMP_MAX, false); | |
169 | if (n < 0) { | |
170 | log_error("Failed to read core dump data: %s", strerror(-n)); | |
171 | r = (int) n; | |
172 | goto finish; | |
173 | } | |
174 | ||
175 | iovec[j].iov_base = p; | |
176 | iovec[j].iov_len = 9 + n; | |
177 | j++; | |
178 | ||
f5e04665 LP |
179 | r = sd_journal_sendv(iovec, j); |
180 | if (r < 0) | |
181 | log_error("Failed to send coredump: %s", strerror(-r)); | |
182 | ||
183 | finish: | |
184 | free(p); | |
185 | free(core_pid); | |
186 | free(core_uid); | |
187 | free(core_gid); | |
188 | free(core_signal); | |
189 | free(core_timestamp); | |
190 | free(core_comm); | |
191 | free(core_exe); | |
192 | free(core_cmdline); | |
193 | free(core_unit); | |
194 | free(core_session); | |
195 | free(core_message); | |
196 | ||
197 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
198 | } |