1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 /* Copyright © 2019 Oracle and/or its affiliates. */
5 /* Generally speaking, the pstore contains a small number of files
6 * that in turn contain a small amount of data. */
10 #include <sys/prctl.h>
11 #include <sys/xattr.h>
14 #include "sd-daemon.h"
15 #include "sd-journal.h"
17 #include "sd-messages.h"
20 #include "alloc-util.h"
21 #include "capability-util.h"
22 #include "cgroup-util.h"
24 #include "conf-parser.h"
26 #include "dirent-util.h"
32 #include "journal-importer.h"
35 #include "main-func.h"
37 #include "parse-util.h"
38 #include "process-util.h"
39 #include "signal-util.h"
40 #include "socket-util.h"
42 #include "sort-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
46 #include "tmpfile-util.h"
47 #include "user-util.h"
50 /* Command line argument handling */
51 typedef enum PStoreStorage
{
53 PSTORE_STORAGE_EXTERNAL
,
54 PSTORE_STORAGE_JOURNAL
,
56 _PSTORE_STORAGE_INVALID
= -EINVAL
,
59 static const char* const pstore_storage_table
[_PSTORE_STORAGE_MAX
] = {
60 [PSTORE_STORAGE_NONE
] = "none",
61 [PSTORE_STORAGE_EXTERNAL
] = "external",
62 [PSTORE_STORAGE_JOURNAL
] = "journal",
65 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(pstore_storage
, PStoreStorage
);
66 static DEFINE_CONFIG_PARSE_ENUM(config_parse_pstore_storage
, pstore_storage
, PStoreStorage
, "Failed to parse storage setting");
68 static PStoreStorage arg_storage
= PSTORE_STORAGE_EXTERNAL
;
70 static bool arg_unlink
= true;
71 static const char *arg_sourcedir
= "/sys/fs/pstore";
72 static const char *arg_archivedir
= "/var/lib/systemd/pstore";
74 static int parse_config(void) {
75 static const ConfigTableItem items
[] = {
76 { "PStore", "Unlink", config_parse_bool
, 0, &arg_unlink
},
77 { "PStore", "Storage", config_parse_pstore_storage
, 0, &arg_storage
},
81 return config_parse_many_nulstr(
82 PKGSYSCONFDIR
"/pstore.conf",
83 CONF_PATHS_NULSTR("systemd/pstore.conf.d"),
85 config_item_table_lookup
, items
,
91 /* File list handling - PStoreEntry is the struct and
92 * and PStoreEntry is the type that contains all info
93 * about a pstore entry. */
94 typedef struct PStoreEntry
{
102 typedef struct PStoreList
{
103 PStoreEntry
*entries
;
107 static void pstore_entries_reset(PStoreList
*list
) {
108 for (size_t i
= 0; i
< list
->n_entries
; i
++)
109 free(list
->entries
[i
].content
);
114 static int compare_pstore_entries(const PStoreEntry
*a
, const PStoreEntry
*b
) {
115 return strcmp(a
->dirent
.d_name
, b
->dirent
.d_name
);
118 static int move_file(PStoreEntry
*pe
, const char *subdir
) {
119 _cleanup_free_
char *ifd_path
= NULL
, *ofd_path
= NULL
;
120 _cleanup_free_
void *field
= NULL
;
121 const char *suffix
, *message
;
122 struct iovec iovec
[2];
128 ifd_path
= path_join(arg_sourcedir
, pe
->dirent
.d_name
);
132 ofd_path
= path_join(arg_archivedir
, subdir
, pe
->dirent
.d_name
);
136 /* Always log to the journal */
137 suffix
= arg_storage
== PSTORE_STORAGE_EXTERNAL
? strjoina(" moved to ", ofd_path
) : (char *)".";
138 message
= strjoina("MESSAGE=PStore ", pe
->dirent
.d_name
, suffix
);
139 iovec
[n_iovec
++] = IOVEC_MAKE_STRING(message
);
141 if (pe
->content_size
> 0) {
144 field_size
= strlen("FILE=") + pe
->content_size
;
145 field
= malloc(field_size
);
148 memcpy(stpcpy(field
, "FILE="), pe
->content
, pe
->content_size
);
149 iovec
[n_iovec
++] = IOVEC_MAKE(field
, field_size
);
152 r
= sd_journal_sendv(iovec
, n_iovec
);
154 return log_error_errno(r
, "Failed to log pstore entry: %m");
156 if (arg_storage
== PSTORE_STORAGE_EXTERNAL
) {
157 /* Move file from pstore to external storage */
158 r
= mkdir_parents(ofd_path
, 0755);
160 return log_error_errno(r
, "Failed to create directory %s: %m", ofd_path
);
161 r
= copy_file_atomic(ifd_path
, ofd_path
, 0600, 0, 0, COPY_REPLACE
);
163 return log_error_errno(r
, "Failed to copy_file_atomic: %s to %s", ifd_path
, ofd_path
);
166 /* If file copied properly, remove it from pstore */
168 (void) unlink(ifd_path
);
175 static int write_dmesg(const char *dmesg
, size_t size
, const char *id
) {
176 _cleanup_(unlink_and_freep
) char *tmp_path
= NULL
;
177 _cleanup_free_
char *ofd_path
= NULL
;
178 _cleanup_close_
int ofd
= -1;
187 ofd_path
= path_join(arg_archivedir
, id
, "dmesg.txt");
191 ofd
= open_tmpfile_linkable(ofd_path
, O_CLOEXEC
|O_CREAT
|O_TRUNC
|O_WRONLY
, &tmp_path
);
193 return log_error_errno(ofd
, "Failed to open temporary file %s: %m", ofd_path
);
194 wr
= write(ofd
, dmesg
, size
);
196 return log_error_errno(errno
, "Failed to store dmesg to %s: %m", ofd_path
);
197 if (wr
!= (ssize_t
)size
)
198 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to store dmesg to %s. %zu bytes are lost.", ofd_path
, size
- wr
);
199 r
= link_tmpfile(ofd
, tmp_path
, ofd_path
);
201 return log_error_errno(r
, "Failed to write temporary file %s: %m", ofd_path
);
202 tmp_path
= mfree(tmp_path
);
207 static void process_dmesg_files(PStoreList
*list
) {
208 /* Move files, reconstruct dmesg.txt */
209 _cleanup_free_
char *dmesg
= NULL
, *dmesg_id
= NULL
;
210 size_t dmesg_size
= 0;
211 bool dmesg_bad
= false;
214 /* Handle each dmesg file: files processed in reverse
215 * order so as to properly reconstruct original dmesg */
216 for (size_t n
= list
->n_entries
; n
> 0; n
--) {
217 bool move_file_and_continue
= false;
218 _cleanup_free_
char *pe_id
= NULL
;
222 pe
= &list
->entries
[n
-1];
226 if (!startswith(pe
->dirent
.d_name
, "dmesg-"))
229 if (endswith(pe
->dirent
.d_name
, ".enc.z")) /* indicates a problem */
230 move_file_and_continue
= true;
231 p
= strrchr(pe
->dirent
.d_name
, '-');
233 move_file_and_continue
= true;
235 if (move_file_and_continue
) {
236 /* A dmesg file on which we do NO additional processing */
237 (void) move_file(pe
, NULL
);
241 /* See if this file is one of a related group of files
242 * in order to reconstruct dmesg */
244 /* When dmesg is written into pstore, it is done so in
245 * small chunks, whatever the exchange buffer size is
246 * with the underlying pstore backend (ie. EFI may be
247 * ~2KiB), which means an example pstore with approximately
248 * 64KB of storage may have up to roughly 32 dmesg files
249 * that could be related, depending upon the size of the
252 * Here we look at the dmesg filename and try to discern
253 * if files are part of a related group, meaning the same
256 * The two known pstore backends are EFI and ERST. These
257 * backends store data in the Common Platform Error
258 * Record, CPER, format. The dmesg- filename contains the
259 * CPER record id, a 64bit number (in decimal notation).
260 * In Linux, the record id is encoded with two digits for
261 * the dmesg part (chunk) number and 3 digits for the
262 * count number. So allowing an additional digit to
263 * compensate for advancing time, this code ignores the
264 * last six digits of the filename in determining the
267 * For the EFI backend, the record id encodes an id in the
268 * upper 32 bits, and a timestamp in the lower 32-bits.
269 * So ignoring the least significant 6 digits has proven
270 * to generally identify related dmesg entries. */
271 #define PSTORE_FILENAME_IGNORE 6
273 /* determine common portion of record id */
274 ++p
; /* move beyond dmesg- */
276 if (plen
> PSTORE_FILENAME_IGNORE
) {
277 pe_id
= memdup_suffix0(p
, plen
- PSTORE_FILENAME_IGNORE
);
283 pe_id
= mfree(pe_id
);
285 /* Now move file from pstore to archive storage */
286 move_file(pe
, pe_id
);
291 /* If the current record id is NOT the same as the
292 * previous record id, then start a new dmesg.txt file */
293 if (!streq_ptr(pe_id
, dmesg_id
)) {
294 /* Encountered a new dmesg group, close out old one, open new one */
295 (void) write_dmesg(dmesg
, dmesg_size
, dmesg_id
);
298 /* now point dmesg_id to storage of pe_id */
299 free_and_replace(dmesg_id
, pe_id
);
302 /* Reconstruction of dmesg is done as a useful courtesy: do not fail, but don't write garbled
304 size_t needed
= strlen(pe
->dirent
.d_name
) + strlen(":\n") + pe
->content_size
+ 1;
305 if (!GREEDY_REALLOC(dmesg
, dmesg_size
+ needed
)) {
311 dmesg_size
+= sprintf(dmesg
+ dmesg_size
, "%s:\n", pe
->dirent
.d_name
);
313 memcpy(dmesg
+ dmesg_size
, pe
->content
, pe
->content_size
);
314 dmesg_size
+= pe
->content_size
;
317 pe_id
= mfree(pe_id
);
321 (void) write_dmesg(dmesg
, dmesg_size
, dmesg_id
);
324 static int list_files(PStoreList
*list
, const char *sourcepath
) {
325 _cleanup_(closedirp
) DIR *dirp
= NULL
;
329 dirp
= opendir(sourcepath
);
331 return log_error_errno(errno
, "Failed to opendir %s: %m", sourcepath
);
333 FOREACH_DIRENT(de
, dirp
, return log_error_errno(errno
, "Failed to iterate through %s: %m", sourcepath
)) {
334 _cleanup_free_
char *ifd_path
= NULL
;
336 ifd_path
= path_join(sourcepath
, de
->d_name
);
340 _cleanup_free_
char *buf
= NULL
;
343 /* Now read contents of pstore file */
344 r
= read_full_virtual_file(ifd_path
, &buf
, &buf_size
);
346 log_warning_errno(r
, "Failed to read file %s, skipping: %m", ifd_path
);
350 if (!GREEDY_REALLOC(list
->entries
, list
->n_entries
+ 1))
353 list
->entries
[list
->n_entries
++] = (PStoreEntry
) {
355 .content
= TAKE_PTR(buf
),
356 .content_size
= buf_size
,
365 static int run(int argc
, char *argv
[]) {
366 _cleanup_(pstore_entries_reset
) PStoreList list
= {};
372 arg_sourcedir
= argv
[1];
373 arg_archivedir
= argv
[2];
375 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
376 "This program takes zero or two arguments.");
378 /* Ignore all parse errors */
379 (void) parse_config();
381 log_debug("Selected storage: %s.", pstore_storage_to_string(arg_storage
));
382 log_debug("Selected unlink: %s.", yes_no(arg_unlink
));
384 if (arg_storage
== PSTORE_STORAGE_NONE
)
385 /* Do nothing, intentionally, leaving pstore untouched */
388 /* Obtain list of files in pstore */
389 r
= list_files(&list
, arg_sourcedir
);
393 /* Handle each pstore file */
394 /* Sort files lexigraphically ascending, generally needed by all */
395 typesafe_qsort(list
.entries
, list
.n_entries
, compare_pstore_entries
);
397 /* Process known file types */
398 process_dmesg_files(&list
);
400 /* Move left over files out of pstore */
401 for (size_t n
= 0; n
< list
.n_entries
; n
++)
402 move_file(&list
.entries
[n
], NULL
);
407 DEFINE_MAIN_FUNCTION(run
);