| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 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 <stdio.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include "sd-journal.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "conf-parser.h" |
| 14 |
#include "copy.h" |
| 15 |
#include "dirent-util.h" |
| 16 |
#include "fd-util.h" |
| 17 |
#include "fileio.h" |
| 18 |
#include "iovec-util.h" |
| 19 |
#include "log.h" |
| 20 |
#include "main-func.h" |
| 21 |
#include "mkdir.h" |
| 22 |
#include "parse-util.h" |
| 23 |
#include "sort-util.h" |
| 24 |
#include "string-table.h" |
| 25 |
#include "string-util.h" |
| 26 |
#include "strv.h" |
| 27 |
|
| 28 |
/* Command line argument handling */ |
| 29 |
typedef enum PStoreStorage { |
| 30 |
PSTORE_STORAGE_NONE, |
| 31 |
PSTORE_STORAGE_EXTERNAL, |
| 32 |
PSTORE_STORAGE_JOURNAL, |
| 33 |
_PSTORE_STORAGE_MAX, |
| 34 |
_PSTORE_STORAGE_INVALID = -EINVAL, |
| 35 |
} PStoreStorage; |
| 36 |
|
| 37 |
static const char* const pstore_storage_table[_PSTORE_STORAGE_MAX] = { |
| 38 |
[PSTORE_STORAGE_NONE] = "none", |
| 39 |
[PSTORE_STORAGE_EXTERNAL] = "external", |
| 40 |
[PSTORE_STORAGE_JOURNAL] = "journal", |
| 41 |
}; |
| 42 |
|
| 43 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(pstore_storage, PStoreStorage); |
| 44 |
static DEFINE_CONFIG_PARSE_ENUM(config_parse_pstore_storage, pstore_storage, PStoreStorage); |
| 45 |
|
| 46 |
static PStoreStorage arg_storage = PSTORE_STORAGE_EXTERNAL; |
| 47 |
|
| 48 |
static bool arg_unlink = true; |
| 49 |
static const char *arg_sourcedir = "/sys/fs/pstore"; |
| 50 |
static const char *arg_archivedir = "/var/lib/systemd/pstore"; |
| 51 |
|
| 52 |
static int parse_config(void) { |
| 53 |
static const ConfigTableItem items[] = { |
| 54 |
{ "PStore", "Unlink", config_parse_bool, 0, &arg_unlink }, |
| 55 |
{ "PStore", "Storage", config_parse_pstore_storage, 0, &arg_storage }, |
| 56 |
{} |
| 57 |
}; |
| 58 |
|
| 59 |
return config_parse_standard_file_with_dropins( |
| 60 |
"systemd/pstore.conf", |
| 61 |
"PStore\0", |
| 62 |
config_item_table_lookup, items, |
| 63 |
CONFIG_PARSE_WARN, |
| 64 |
/* userdata= */ NULL); |
| 65 |
} |
| 66 |
|
| 67 |
/* File list handling - PStoreEntry is the struct and |
| 68 |
* and PStoreEntry is the type that contains all info |
| 69 |
* about a pstore entry. */ |
| 70 |
typedef struct PStoreEntry { |
| 71 |
struct dirent dirent; |
| 72 |
bool is_binary; |
| 73 |
bool handled; |
| 74 |
char *content; |
| 75 |
size_t content_size; |
| 76 |
} PStoreEntry; |
| 77 |
|
| 78 |
typedef struct PStoreList { |
| 79 |
PStoreEntry *entries; |
| 80 |
size_t n_entries; |
| 81 |
} PStoreList; |
| 82 |
|
| 83 |
static void pstore_entries_reset(PStoreList *list) { |
| 84 |
for (size_t i = 0; i < list->n_entries; i++) |
| 85 |
free(list->entries[i].content); |
| 86 |
free(list->entries); |
| 87 |
list->n_entries = 0; |
| 88 |
} |
| 89 |
|
| 90 |
static int compare_pstore_entries(const PStoreEntry *a, const PStoreEntry *b) { |
| 91 |
return strcmp(a->dirent.d_name, b->dirent.d_name); |
| 92 |
} |
| 93 |
|
| 94 |
static int move_file(PStoreEntry *pe, const char *subdir1, const char *subdir2) { |
| 95 |
_cleanup_free_ char *ifd_path = NULL, *ofd_path = NULL; |
| 96 |
_cleanup_free_ void *field = NULL; |
| 97 |
const char *suffix, *message; |
| 98 |
struct iovec iovec[2]; |
| 99 |
int n_iovec = 0, r; |
| 100 |
|
| 101 |
if (pe->handled) |
| 102 |
return 0; |
| 103 |
|
| 104 |
ifd_path = path_join(arg_sourcedir, pe->dirent.d_name); |
| 105 |
if (!ifd_path) |
| 106 |
return log_oom(); |
| 107 |
|
| 108 |
ofd_path = path_join(arg_archivedir, subdir1, subdir2, pe->dirent.d_name); |
| 109 |
if (!ofd_path) |
| 110 |
return log_oom(); |
| 111 |
|
| 112 |
/* Always log to the journal */ |
| 113 |
suffix = arg_storage == PSTORE_STORAGE_EXTERNAL ? strjoina(" moved to ", ofd_path) : (char *)"."; |
| 114 |
message = strjoina("MESSAGE=PStore ", pe->dirent.d_name, suffix); |
| 115 |
iovec[n_iovec++] = IOVEC_MAKE_STRING(message); |
| 116 |
|
| 117 |
if (pe->content_size > 0) { |
| 118 |
size_t field_size; |
| 119 |
|
| 120 |
field_size = strlen("FILE=") + pe->content_size; |
| 121 |
field = malloc(field_size); |
| 122 |
if (!field) |
| 123 |
return log_oom(); |
| 124 |
memcpy(stpcpy(field, "FILE="), pe->content, pe->content_size); |
| 125 |
iovec[n_iovec++] = IOVEC_MAKE(field, field_size); |
| 126 |
} |
| 127 |
|
| 128 |
r = sd_journal_sendv(iovec, n_iovec); |
| 129 |
if (r < 0) |
| 130 |
return log_error_errno(r, "Failed to log pstore entry: %m"); |
| 131 |
|
| 132 |
if (arg_storage == PSTORE_STORAGE_EXTERNAL) { |
| 133 |
/* Move file from pstore to external storage */ |
| 134 |
r = mkdir_parents(ofd_path, 0755); |
| 135 |
if (r < 0) |
| 136 |
return log_error_errno(r, "Failed to create directory %s: %m", ofd_path); |
| 137 |
r = copy_file_atomic(ifd_path, ofd_path, 0600, COPY_REPLACE); |
| 138 |
if (r < 0) |
| 139 |
return log_error_errno(r, "Failed to copy_file_atomic: %s to %s", ifd_path, ofd_path); |
| 140 |
} |
| 141 |
|
| 142 |
/* If file copied properly, remove it from pstore */ |
| 143 |
if (arg_unlink) |
| 144 |
(void) unlink(ifd_path); |
| 145 |
|
| 146 |
pe->handled = true; |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
static int append_dmesg(PStoreEntry *pe, const char *subdir1, const char *subdir2) { |
| 152 |
/* Append dmesg chunk to end, create if needed */ |
| 153 |
_cleanup_free_ char *ofd_path = NULL; |
| 154 |
_cleanup_close_ int ofd = -EBADF; |
| 155 |
ssize_t wr; |
| 156 |
|
| 157 |
assert(pe); |
| 158 |
|
| 159 |
if (arg_storage != PSTORE_STORAGE_EXTERNAL) |
| 160 |
return 0; |
| 161 |
|
| 162 |
if (pe->content_size == 0) |
| 163 |
return 0; |
| 164 |
|
| 165 |
ofd_path = path_join(arg_archivedir, subdir1, subdir2, "dmesg.txt"); |
| 166 |
if (!ofd_path) |
| 167 |
return log_oom(); |
| 168 |
|
| 169 |
ofd = open(ofd_path, O_CREAT|O_NOFOLLOW|O_NOCTTY|O_CLOEXEC|O_APPEND|O_WRONLY, 0640); |
| 170 |
if (ofd < 0) |
| 171 |
return log_error_errno(ofd, "Failed to open file %s: %m", ofd_path); |
| 172 |
wr = write(ofd, pe->content, pe->content_size); |
| 173 |
if (wr < 0) |
| 174 |
return log_error_errno(errno, "Failed to store dmesg to %s: %m", ofd_path); |
| 175 |
if ((size_t)wr != pe->content_size) |
| 176 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to store dmesg to %s. %zu bytes are lost.", ofd_path, pe->content_size - wr); |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
static int process_dmesg_files(PStoreList *list) { |
| 182 |
/* Move files, reconstruct dmesg.txt */ |
| 183 |
_cleanup_free_ char *erst_subdir = NULL; |
| 184 |
unsigned long long last_record_id = 0; |
| 185 |
|
| 186 |
/* When dmesg is written into pstore, it is done so in small chunks, whatever the exchange buffer |
| 187 |
* size is with the underlying pstore backend (ie. EFI may be ~2KiB), which means an example |
| 188 |
* pstore with approximately 64KB of storage may have up to roughly 32 dmesg files, some likely |
| 189 |
* related. |
| 190 |
* |
| 191 |
* Here we look at the dmesg filename and try to discern if files are part of a related group, |
| 192 |
* meaning the same original dmesg. |
| 193 |
* |
| 194 |
* The dmesg- filename contains the backend-type and the Common Platform Error Record, CPER, |
| 195 |
* record id, a 64-bit number. |
| 196 |
* |
| 197 |
* Files are processed in reverse lexicographical order so as to properly reconstruct original dmesg. */ |
| 198 |
|
| 199 |
for (size_t n = list->n_entries; n > 0; n--) { |
| 200 |
PStoreEntry *pe; |
| 201 |
char *p; |
| 202 |
|
| 203 |
pe = &list->entries[n-1]; |
| 204 |
|
| 205 |
if (pe->handled) |
| 206 |
continue; |
| 207 |
if (endswith(pe->dirent.d_name, ".enc.z")) /* indicates a problem */ |
| 208 |
continue; |
| 209 |
if (!startswith(pe->dirent.d_name, "dmesg-")) |
| 210 |
continue; |
| 211 |
|
| 212 |
/* The linux kernel changed the prefix from dmesg-efi- to dmesg-efi_pstore- |
| 213 |
* so now we have to handle both cases. */ |
| 214 |
if ((p = STARTSWITH_SET(pe->dirent.d_name, "dmesg-efi-", "dmesg-efi_pstore-"))) { |
| 215 |
/* For the EFI backend, the 3 least significant digits of record id encodes a |
| 216 |
* "count" number, the next 2 least significant digits for the dmesg part |
| 217 |
* (chunk) number, and the remaining digits as the timestamp. See |
| 218 |
* linux/drivers/firmware/efi/efi-pstore.c in efi_pstore_write(). */ |
| 219 |
_cleanup_free_ char *subdir1 = NULL, *subdir2 = NULL; |
| 220 |
size_t plen = strlen(p); |
| 221 |
|
| 222 |
if (plen < 6) |
| 223 |
continue; |
| 224 |
|
| 225 |
/* Extract base record id */ |
| 226 |
subdir1 = strndup(p, plen - 5); |
| 227 |
if (!subdir1) |
| 228 |
return log_oom(); |
| 229 |
/* Extract "count" field */ |
| 230 |
subdir2 = strndup(p + plen - 3, 3); |
| 231 |
if (!subdir2) |
| 232 |
return log_oom(); |
| 233 |
|
| 234 |
/* Now move file from pstore to archive storage */ |
| 235 |
(void) move_file(pe, subdir1, subdir2); |
| 236 |
|
| 237 |
/* Append to the dmesg */ |
| 238 |
(void) append_dmesg(pe, subdir1, subdir2); |
| 239 |
} else if ((p = startswith(pe->dirent.d_name, "dmesg-erst-"))) { |
| 240 |
/* For the ERST backend, the record is a monotonically increasing number, seeded as |
| 241 |
* a timestamp. See linux/drivers/acpi/apei/erst.c in erst_writer(). */ |
| 242 |
unsigned long long record_id; |
| 243 |
|
| 244 |
if (safe_atollu_full(p, 10, &record_id) < 0) |
| 245 |
continue; |
| 246 |
if (last_record_id - 1 != record_id) |
| 247 |
/* A discontinuity in the number has been detected, this current record id |
| 248 |
* will become the directory name for all pieces of the dmesg in this |
| 249 |
* series. */ |
| 250 |
if (free_and_strdup(&erst_subdir, p) < 0) |
| 251 |
return log_oom(); |
| 252 |
|
| 253 |
/* Now move file from pstore to archive storage */ |
| 254 |
(void) move_file(pe, erst_subdir, NULL); |
| 255 |
|
| 256 |
/* Append to the dmesg */ |
| 257 |
(void) append_dmesg(pe, erst_subdir, NULL); |
| 258 |
|
| 259 |
/* Update, but keep erst_subdir for next file */ |
| 260 |
last_record_id = record_id; |
| 261 |
} else |
| 262 |
log_debug("Unknown backend, ignoring \"%s\".", pe->dirent.d_name); |
| 263 |
} |
| 264 |
|
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
static int list_files(PStoreList *list, const char *sourcepath) { |
| 269 |
_cleanup_closedir_ DIR *dirp = NULL; |
| 270 |
int r; |
| 271 |
|
| 272 |
dirp = opendir(sourcepath); |
| 273 |
if (!dirp) |
| 274 |
return log_error_errno(errno, "Failed to opendir %s: %m", sourcepath); |
| 275 |
|
| 276 |
FOREACH_DIRENT(de, dirp, return log_error_errno(errno, "Failed to iterate through %s: %m", sourcepath)) { |
| 277 |
_cleanup_free_ char *ifd_path = NULL; |
| 278 |
|
| 279 |
ifd_path = path_join(sourcepath, de->d_name); |
| 280 |
if (!ifd_path) |
| 281 |
return log_oom(); |
| 282 |
|
| 283 |
_cleanup_free_ char *buf = NULL; |
| 284 |
size_t buf_size; |
| 285 |
|
| 286 |
/* Now read contents of pstore file */ |
| 287 |
r = read_full_virtual_file(ifd_path, &buf, &buf_size); |
| 288 |
if (r < 0) { |
| 289 |
log_warning_errno(r, "Failed to read file %s, skipping: %m", ifd_path); |
| 290 |
continue; |
| 291 |
} |
| 292 |
|
| 293 |
if (!GREEDY_REALLOC(list->entries, list->n_entries + 1)) |
| 294 |
return log_oom(); |
| 295 |
|
| 296 |
list->entries[list->n_entries++] = (PStoreEntry) { |
| 297 |
.dirent = *de, |
| 298 |
.content = TAKE_PTR(buf), |
| 299 |
.content_size = buf_size, |
| 300 |
.is_binary = true, |
| 301 |
.handled = false, |
| 302 |
}; |
| 303 |
} |
| 304 |
|
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
static int run(int argc, char *argv[]) { |
| 309 |
_cleanup_(pstore_entries_reset) PStoreList list = {}; |
| 310 |
int r; |
| 311 |
|
| 312 |
log_setup(); |
| 313 |
|
| 314 |
if (argc == 3) { |
| 315 |
arg_sourcedir = argv[1]; |
| 316 |
arg_archivedir = argv[2]; |
| 317 |
} else if (argc > 1) |
| 318 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 319 |
"This program takes zero or two arguments."); |
| 320 |
|
| 321 |
/* Ignore all parse errors */ |
| 322 |
(void) parse_config(); |
| 323 |
|
| 324 |
log_debug("Selected storage: %s.", pstore_storage_to_string(arg_storage)); |
| 325 |
log_debug("Selected unlink: %s.", yes_no(arg_unlink)); |
| 326 |
|
| 327 |
if (arg_storage == PSTORE_STORAGE_NONE) |
| 328 |
/* Do nothing, intentionally, leaving pstore untouched */ |
| 329 |
return 0; |
| 330 |
|
| 331 |
/* Obtain list of files in pstore */ |
| 332 |
r = list_files(&list, arg_sourcedir); |
| 333 |
if (r < 0) |
| 334 |
return r; |
| 335 |
|
| 336 |
/* Handle each pstore file */ |
| 337 |
/* Sort files lexicographically ascending, generally needed by all */ |
| 338 |
typesafe_qsort(list.entries, list.n_entries, compare_pstore_entries); |
| 339 |
|
| 340 |
/* Process known file types */ |
| 341 |
(void) process_dmesg_files(&list); |
| 342 |
|
| 343 |
/* Move left over files out of pstore */ |
| 344 |
for (size_t n = 0; n < list.n_entries; n++) |
| 345 |
(void) move_file(&list.entries[n], NULL, NULL); |
| 346 |
|
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
DEFINE_MAIN_FUNCTION(run); |