]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/pstore/pstore.c
pstore: refuse to run if arguments are specified
[thirdparty/systemd.git] / src / pstore / pstore.c
CommitLineData
9b4abc69
ED
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3/* Copyright © 2019 Oracle and/or its affiliates. */
4
5/* Generally speaking, the pstore contains a small number of files
6 * that in turn contain a small amount of data. */
7#include <errno.h>
8#include <stdio.h>
9#include <stdio_ext.h>
10#include <sys/prctl.h>
11#include <sys/xattr.h>
12#include <unistd.h>
13
14#include "sd-daemon.h"
15#include "sd-journal.h"
16#include "sd-login.h"
17#include "sd-messages.h"
18
19#include "acl-util.h"
20#include "alloc-util.h"
21#include "capability-util.h"
22#include "cgroup-util.h"
23#include "compress.h"
24#include "conf-parser.h"
25#include "copy.h"
26#include "dirent-util.h"
27#include "escape.h"
28#include "fd-util.h"
29#include "fileio.h"
30#include "fs-util.h"
31#include "io-util.h"
32#include "journal-importer.h"
33#include "log.h"
34#include "macro.h"
35#include "main-func.h"
36#include "missing.h"
37#include "mkdir.h"
38#include "parse-util.h"
39#include "process-util.h"
40#include "signal-util.h"
41#include "socket-util.h"
42#include "special.h"
43#include "sort-util.h"
44#include "string-table.h"
45#include "string-util.h"
46#include "strv.h"
47#include "tmpfile-util.h"
48#include "user-util.h"
49#include "util.h"
50
51/* Command line argument handling */
52typedef enum PStoreStorage {
53 PSTORE_STORAGE_NONE,
54 PSTORE_STORAGE_EXTERNAL,
55 PSTORE_STORAGE_JOURNAL,
56 _PSTORE_STORAGE_MAX,
57 _PSTORE_STORAGE_INVALID = -1
58} PStoreStorage;
59
60static const char* const pstore_storage_table[_PSTORE_STORAGE_MAX] = {
61 [PSTORE_STORAGE_NONE] = "none",
62 [PSTORE_STORAGE_EXTERNAL] = "external",
63 [PSTORE_STORAGE_JOURNAL] = "journal",
64};
65
66DEFINE_PRIVATE_STRING_TABLE_LOOKUP(pstore_storage, PStoreStorage);
67static DEFINE_CONFIG_PARSE_ENUM(config_parse_pstore_storage, pstore_storage, PStoreStorage, "Failed to parse storage setting");
68
69static PStoreStorage arg_storage = PSTORE_STORAGE_EXTERNAL;
70
71static bool arg_unlink = true;
72static const char *arg_sourcedir = "/sys/fs/pstore";
73static const char *arg_archivedir = "/var/lib/systemd/pstore";
74
75static int parse_config(void) {
76 static const ConfigTableItem items[] = {
77 { "PStore", "Unlink", config_parse_bool, 0, &arg_unlink },
78 { "PStore", "Storage", config_parse_pstore_storage, 0, &arg_storage },
79 {}
80 };
81
82 return config_parse_many_nulstr(PKGSYSCONFDIR "/pstore.conf",
83 CONF_PATHS_NULSTR("systemd/pstore.conf.d"),
84 "PStore\0",
85 config_item_table_lookup, items,
86 CONFIG_PARSE_WARN, NULL);
87}
88
89/* File list handling - PStoreEntry is the struct and
90 * and PStoreEntry is the type that contains all info
91 * about a pstore entry. */
92typedef struct PStoreEntry {
93 struct dirent dirent;
94 bool is_binary;
95 bool handled;
96 char *content;
97 size_t content_size;
98} PStoreEntry;
99
100typedef struct PStoreList {
101 PStoreEntry *entries;
102 size_t n_entries;
103 size_t n_entries_allocated;
104} PStoreList;
105
106static void pstore_entries_reset(PStoreList *list) {
107 for (size_t i = 0; i < list->n_entries; i++)
108 free(list->entries[i].content);
109 free(list->entries);
110 list->n_entries = 0;
111}
112
113static int compare_pstore_entries(const void *_a, const void *_b) {
114 PStoreEntry *a = (PStoreEntry *)_a, *b = (PStoreEntry *)_b;
115 return strcmp(a->dirent.d_name, b->dirent.d_name);
116}
117
118static int move_file(PStoreEntry *pe, const char *subdir) {
119 _cleanup_free_ char *ifd_path = NULL;
120 _cleanup_free_ char *ofd_path = NULL;
121 int r = 0;
122 struct iovec iovec[2] = {};
123 int n_iovec = 0;
124 _cleanup_free_ void *field = NULL;
125 const char *suffix = NULL;
126 size_t field_size;
127
128 if (pe->handled)
129 return 0;
130
131 ifd_path = path_join(arg_sourcedir, pe->dirent.d_name);
132 if (!ifd_path)
133 return log_oom();
134
135 ofd_path = path_join(arg_archivedir, subdir, pe->dirent.d_name);
136 if (!ofd_path)
137 return log_oom();
138
139 /* Always log to the journal */
140 suffix = arg_storage == PSTORE_STORAGE_EXTERNAL ? strjoina(" moved to ", ofd_path) : (char *)".";
141 field = strjoina("MESSAGE=PStore ", pe->dirent.d_name, suffix);
142 iovec[n_iovec++] = IOVEC_MAKE_STRING(field);
143
144 field_size = strlen("FILE=") + pe->content_size;
145 field = malloc(field_size);
146 if (!field)
147 return log_oom();
148 memcpy(stpcpy(field, "FILE="), pe->content, pe->content_size);
149 iovec[n_iovec++] = IOVEC_MAKE(field, field_size);
150
151 r = sd_journal_sendv(iovec, n_iovec);
152 if (r < 0)
153 return log_error_errno(r, "Failed to log pstore entry: %m");
154
155 if (arg_storage == PSTORE_STORAGE_EXTERNAL) {
156 /* Move file from pstore to external storage */
157 r = mkdir_parents(ofd_path, 0755);
158 if (r < 0)
159 return log_error_errno(r, "Failed to create directoy %s: %m", ofd_path);
160 r = copy_file_atomic(ifd_path, ofd_path, 0600, 0, 0, COPY_REPLACE);
161 if (r < 0)
162 return log_error_errno(r, "Failed to copy_file_atomic: %s to %s", ifd_path, ofd_path);
163 }
164
165 /* If file copied properly, remove it from pstore */
166 if (arg_unlink)
167 (void) unlink(ifd_path);
168
169 pe->handled = true;
170
171 return 0;
172}
173
174static int write_dmesg(const char *dmesg, size_t size, const char *id) {
175 _cleanup_(unlink_and_freep) char *ofd_path = NULL;
176 _cleanup_free_ char *tmp_path = NULL;
177 _cleanup_close_ int ofd = -1;
178 ssize_t wr;
179 int r;
180
181 if (isempty(dmesg) || size == 0)
182 return 0;
183
184 /* log_info("Record ID %s", id); */
185
186 ofd_path = path_join(arg_archivedir, id, "dmesg.txt");
187 if (!ofd_path)
188 return log_oom();
189
190 ofd = open_tmpfile_linkable(ofd_path, O_CLOEXEC|O_CREAT|O_TRUNC|O_WRONLY, &tmp_path);
191 if (ofd < 0)
192 return log_error_errno(ofd, "Failed to open temporary file %s: %m", ofd_path);
193 wr = write(ofd, dmesg, size);
194 if (wr < 0)
195 return log_error_errno(errno, "Failed to store dmesg to %s: %m", ofd_path);
196 if (wr != (ssize_t)size)
197 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to store dmesg to %s. %zu bytes are lost.", ofd_path, size - wr);
198 r = link_tmpfile(ofd, tmp_path, ofd_path);
199 if (r < 0)
200 return log_error_errno(r, "Failed to write temporary file %s: %m", ofd_path);
201 ofd_path = mfree(ofd_path);
202
203 return 0;
204}
205
206static void process_dmesg_files(PStoreList *list) {
207 /* Move files, reconstruct dmesg.txt */
208 PStoreEntry *pe;
209 _cleanup_free_ char *dmesg = NULL;
210 size_t dmesg_size = 0;
211 _cleanup_free_ char *dmesg_id = NULL;
212
213 /* Handle each dmesg file: files processed in reverse
214 * order so as to properly reconstruct original dmesg */
215 for (size_t n = list->n_entries; n > 0; n--) {
216 bool move_file_and_continue = false;
217 _cleanup_free_ char *pe_id = NULL;
218 char *p;
219 size_t plen;
220
221 pe = &list->entries[n-1];
222
223 if (pe->handled)
224 continue;
225 if (!startswith(pe->dirent.d_name, "dmesg-"))
226 continue;
227
228 if (endswith(pe->dirent.d_name, ".enc.z")) /* indicates a problem */
229 move_file_and_continue = true;
230 p = strrchr(pe->dirent.d_name, '-');
231 if (!p)
232 move_file_and_continue = true;
233
234 if (move_file_and_continue) {
235 /* A dmesg file on which we do NO additional processing */
236 (void) move_file(pe, NULL);
237 continue;
238 }
239
240 /* See if this file is one of a related group of files
241 * in order to reconstruct dmesg */
242
243 /* When dmesg is written into pstore, it is done so in
244 * small chunks, whatever the exchange buffer size is
245 * with the underlying pstore backend (ie. EFI may be
246 * ~2KiB), which means an example pstore with approximately
247 * 64KB of storage may have up to roughly 32 dmesg files
248 * that could be related, depending upon the size of the
249 * original dmesg.
250 *
251 * Here we look at the dmesg filename and try to discern
252 * if files are part of a related group, meaning the same
253 * original dmesg.
254 *
255 * The two known pstore backends are EFI and ERST. These
256 * backends store data in the Common Platform Error
257 * Record, CPER, format. The dmesg- filename contains the
258 * CPER record id, a 64bit number (in decimal notation).
259 * In Linux, the record id is encoded with two digits for
260 * the dmesg part (chunk) number and 3 digits for the
261 * count number. So allowing an additional digit to
262 * compensate for advancing time, this code ignores the
263 * last six digits of the filename in determining the
264 * record id.
265 *
266 * For the EFI backend, the record id encodes an id in the
267 * upper 32 bits, and a timestamp in the lower 32-bits.
268 * So ignoring the least significant 6 digits has proven
269 * to generally identify related dmesg entries. */
270#define PSTORE_FILENAME_IGNORE 6
271
272 /* determine common portion of record id */
273 ++p; /* move beyond dmesg- */
274 plen = strlen(p);
275 if (plen > PSTORE_FILENAME_IGNORE) {
276 pe_id = memdup_suffix0(p, plen - PSTORE_FILENAME_IGNORE);
277 if (!pe_id) {
278 log_oom();
279 return;
280 }
281 } else
282 pe_id = mfree(pe_id);
283
284 /* Now move file from pstore to archive storage */
285 move_file(pe, pe_id);
286
287 /* If the current record id is NOT the same as the
288 * previous record id, then start a new dmesg.txt file */
289 if (!pe_id || !dmesg_id || !streq(pe_id, dmesg_id)) {
290 /* Encountered a new dmesg group, close out old one, open new one */
291 if (dmesg) {
292 (void) write_dmesg(dmesg, dmesg_size, dmesg_id);
293 dmesg = mfree(dmesg);
294 dmesg_size = 0;
295 }
296
297 /* now point dmesg_id to storage of pe_id */
298 free_and_replace(dmesg_id, pe_id);
299 }
300
301 /* Reconstruction of dmesg is done as a useful courtesy, do not log errors */
302 dmesg = realloc(dmesg, dmesg_size + strlen(pe->dirent.d_name) + strlen(":\n") + pe->content_size + 1);
303 if (dmesg) {
304 dmesg_size += sprintf(&dmesg[dmesg_size], "%s:\n", pe->dirent.d_name);
305 if (pe->content) {
306 memcpy(&dmesg[dmesg_size], pe->content, pe->content_size);
307 dmesg_size += pe->content_size;
308 }
309 }
310
311 pe_id = mfree(pe_id);
312 }
313 if (dmesg)
314 (void) write_dmesg(dmesg, dmesg_size, dmesg_id);
315}
316
317static int list_files(PStoreList *list, const char *sourcepath) {
318 _cleanup_(closedirp) DIR *dirp = NULL;
319 struct dirent *de;
320 int r = 0;
321
322 dirp = opendir(sourcepath);
323 if (!dirp)
324 return log_error_errno(errno, "Failed to opendir %s: %m", sourcepath);
325
326 FOREACH_DIRENT(de, dirp, return log_error_errno(errno, "Failed to iterate through %s: %m", sourcepath)) {
327 _cleanup_free_ char *ifd_path = NULL;
328
329 ifd_path = path_join(sourcepath, de->d_name);
330 if (!ifd_path)
331 return log_oom();
332
333 _cleanup_free_ char *buf = NULL;
334 size_t buf_size;
335
336 /* Now read contents of pstore file */
337 r = read_full_file(ifd_path, &buf, &buf_size);
338 if (r < 0) {
339 log_warning_errno(r, "Failed to read file %s: %m", ifd_path);
340 continue;
341 }
342
343 if (!GREEDY_REALLOC(list->entries, list->n_entries_allocated, list->n_entries + 1))
344 return log_oom();
345
346 list->entries[list->n_entries++] = (PStoreEntry) {
347 .dirent = *de,
348 .content = TAKE_PTR(buf),
349 .content_size = buf_size,
350 .is_binary = true,
351 .handled = false,
352 };
353 }
354
355 return r;
356}
357
358static int run(int argc, char *argv[]) {
359 _cleanup_(pstore_entries_reset) PStoreList list = {};
360 int r;
361
362 log_open();
363
22d6bea8
ZJS
364 if (argc > 1)
365 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
366 "This program takes no arguments.");
367
9b4abc69
ED
368 /* Ignore all parse errors */
369 (void) parse_config();
370
371 log_debug("Selected storage '%s'.", pstore_storage_to_string(arg_storage));
372 log_debug("Selected Unlink '%d'.", arg_unlink);
373
374 if (arg_storage == PSTORE_STORAGE_NONE)
375 /* Do nothing, intentionally, leaving pstore untouched */
376 return 0;
377
378 /* Obtain list of files in pstore */
379 r = list_files(&list, arg_sourcedir);
380 if (r < 0)
381 return r;
382
383 /* Handle each pstore file */
384 /* Sort files lexigraphically ascending, generally needed by all */
385 qsort_safe(list.entries, list.n_entries, sizeof(PStoreEntry), compare_pstore_entries);
386
387 /* Process known file types */
388 process_dmesg_files(&list);
389
390 /* Move left over files out of pstore */
391 for (size_t n = 0; n < list.n_entries; n++)
392 move_file(&list.entries[n], NULL);
393
394 return 0;
395}
396
397DEFINE_MAIN_FUNCTION(run);