| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "build.h" |
| 7 |
#include "devnum-util.h" |
| 8 |
#include "efivars.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "find-esp.h" |
| 11 |
#include "format-table.h" |
| 12 |
#include "fs-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "main-func.h" |
| 15 |
#include "options.h" |
| 16 |
#include "parse-util.h" |
| 17 |
#include "path-util.h" |
| 18 |
#include "pretty-print.h" |
| 19 |
#include "stdio-util.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "sync-util.h" |
| 23 |
#include "verbs.h" |
| 24 |
#include "virt.h" |
| 25 |
|
| 26 |
static char **arg_path = NULL; |
| 27 |
|
| 28 |
STATIC_DESTRUCTOR_REGISTER(arg_path, strv_freep); |
| 29 |
|
| 30 |
typedef enum Status { |
| 31 |
STATUS_GOOD, |
| 32 |
STATUS_BAD, |
| 33 |
STATUS_INDETERMINATE, |
| 34 |
} Status; |
| 35 |
|
| 36 |
static int help(void) { |
| 37 |
_cleanup_free_ char *link = NULL; |
| 38 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 39 |
int r; |
| 40 |
|
| 41 |
r = terminal_urlify_man("systemd-bless-boot.service", "8", &link); |
| 42 |
if (r < 0) |
| 43 |
return log_oom(); |
| 44 |
|
| 45 |
r = option_parser_get_help_table(&options); |
| 46 |
if (r < 0) |
| 47 |
return r; |
| 48 |
|
| 49 |
r = verbs_get_help_table(&verbs); |
| 50 |
if (r < 0) |
| 51 |
return r; |
| 52 |
|
| 53 |
(void) table_sync_column_widths(0, options, verbs); |
| 54 |
|
| 55 |
printf("%s [OPTIONS...] COMMAND\n" |
| 56 |
"\n%sMark the boot process as good or bad.%s\n" |
| 57 |
"\nCommands:\n", |
| 58 |
program_invocation_short_name, |
| 59 |
ansi_highlight(), |
| 60 |
ansi_normal()); |
| 61 |
r = table_print_or_warn(verbs); |
| 62 |
if (r < 0) |
| 63 |
return r; |
| 64 |
|
| 65 |
printf("\nOptions:\n"); |
| 66 |
r = table_print_or_warn(options); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
|
| 70 |
printf("\nSee the %s for details.\n", link); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
VERB_COMMON_HELP_HIDDEN(help); |
| 75 |
|
| 76 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 77 |
int r; |
| 78 |
|
| 79 |
assert(argc >= 0); |
| 80 |
assert(argv); |
| 81 |
|
| 82 |
OptionParser opts = { argc, argv }; |
| 83 |
|
| 84 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 85 |
switch (c) { |
| 86 |
OPTION_COMMON_HELP: |
| 87 |
return help(); |
| 88 |
|
| 89 |
OPTION_COMMON_VERSION: |
| 90 |
return version(); |
| 91 |
|
| 92 |
OPTION_LONG("path", "PATH", "Path to the $BOOT partition (may be used multiple times)"): |
| 93 |
r = strv_extend(&arg_path, opts.arg); |
| 94 |
if (r < 0) |
| 95 |
return log_oom(); |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
*ret_args = option_parser_get_args(&opts); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
static int acquire_path(void) { |
| 104 |
_cleanup_free_ char *esp_path = NULL, *xbootldr_path = NULL; |
| 105 |
dev_t esp_devid = 0, xbootldr_devid = 0; |
| 106 |
char **a; |
| 107 |
int r; |
| 108 |
|
| 109 |
if (!strv_isempty(arg_path)) |
| 110 |
return 0; |
| 111 |
|
| 112 |
r = find_esp_and_warn_full( |
| 113 |
/* root= */ NULL, |
| 114 |
/* path= */ NULL, |
| 115 |
/* unprivileged_mode= */ false, |
| 116 |
&esp_path, |
| 117 |
/* ret_fd= */ NULL, |
| 118 |
/* ret_part= */ NULL, |
| 119 |
/* ret_pstart= */ NULL, |
| 120 |
/* ret_psize= */ NULL, |
| 121 |
/* ret_uuid= */ NULL, |
| 122 |
&esp_devid); |
| 123 |
if (r < 0 && r != -ENOKEY) /* ENOKEY means not found, and is the only error the function won't log about on its own */ |
| 124 |
return r; |
| 125 |
|
| 126 |
r = find_xbootldr_and_warn_full( |
| 127 |
/* root= */ NULL, |
| 128 |
/* path= */ NULL, |
| 129 |
/* unprivileged_mode= */ false, |
| 130 |
&xbootldr_path, |
| 131 |
/* ret_fd= */ NULL, |
| 132 |
/* ret_uuid= */ NULL, |
| 133 |
&xbootldr_devid); |
| 134 |
if (r < 0 && r != -ENOKEY) |
| 135 |
return r; |
| 136 |
|
| 137 |
if (!esp_path && !xbootldr_path) |
| 138 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), |
| 139 |
"Couldn't find $BOOT partition. It is recommended to mount it to /boot.\n" |
| 140 |
"Alternatively, use --path= to specify path to mount point."); |
| 141 |
|
| 142 |
if (esp_path && xbootldr_path && !devnum_set_and_equal(esp_devid, xbootldr_devid)) /* in case the two paths refer to the same inode, suppress one */ |
| 143 |
a = strv_new(esp_path, xbootldr_path); |
| 144 |
else if (esp_path) |
| 145 |
a = strv_new(esp_path); |
| 146 |
else |
| 147 |
a = strv_new(xbootldr_path); |
| 148 |
if (!a) |
| 149 |
return log_oom(); |
| 150 |
|
| 151 |
strv_free_and_replace(arg_path, a); |
| 152 |
|
| 153 |
if (DEBUG_LOGGING) { |
| 154 |
_cleanup_free_ char *j = NULL; |
| 155 |
|
| 156 |
j = strv_join(arg_path, ":"); |
| 157 |
log_debug("Using %s as boot loader drop-in search path.", strna(j)); |
| 158 |
} |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static int parse_counter( |
| 164 |
const char *path, |
| 165 |
const char **p, |
| 166 |
uint64_t *ret_left, |
| 167 |
uint64_t *ret_done) { |
| 168 |
|
| 169 |
uint64_t left, done; |
| 170 |
const char *z, *e; |
| 171 |
size_t k; |
| 172 |
int r; |
| 173 |
|
| 174 |
assert(path); |
| 175 |
assert(p); |
| 176 |
|
| 177 |
e = *p; |
| 178 |
assert(e); |
| 179 |
assert(*e == '+'); |
| 180 |
|
| 181 |
e++; |
| 182 |
|
| 183 |
k = strspn(e, DIGITS); |
| 184 |
if (k == 0) |
| 185 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 186 |
"Can't parse empty 'tries left' counter from LoaderBootCountPath: %s", |
| 187 |
path); |
| 188 |
|
| 189 |
z = strndupa_safe(e, k); |
| 190 |
r = safe_atou64(z, &left); |
| 191 |
if (r < 0) |
| 192 |
return log_error_errno(r, "Failed to parse 'tries left' counter from LoaderBootCountPath: %s", path); |
| 193 |
|
| 194 |
e += k; |
| 195 |
|
| 196 |
if (*e == '-') { |
| 197 |
e++; |
| 198 |
|
| 199 |
k = strspn(e, DIGITS); |
| 200 |
if (k == 0) /* If there's a "-" there also needs to be at least one digit */ |
| 201 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 202 |
"Can't parse empty 'tries done' counter from LoaderBootCountPath: %s", |
| 203 |
path); |
| 204 |
|
| 205 |
z = strndupa_safe(e, k); |
| 206 |
r = safe_atou64(z, &done); |
| 207 |
if (r < 0) |
| 208 |
return log_error_errno(r, "Failed to parse 'tries done' counter from LoaderBootCountPath: %s", path); |
| 209 |
|
| 210 |
e += k; |
| 211 |
} else |
| 212 |
done = 0; |
| 213 |
|
| 214 |
if (done == 0) |
| 215 |
log_warning("The 'tries done' counter is currently at zero. This can't really be, after all we are running, and this boot must hence count as one. Proceeding anyway."); |
| 216 |
|
| 217 |
*p = e; |
| 218 |
|
| 219 |
if (ret_left) |
| 220 |
*ret_left = left; |
| 221 |
|
| 222 |
if (ret_done) |
| 223 |
*ret_done = done; |
| 224 |
|
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
static int acquire_boot_count_path( |
| 229 |
char **ret_path, |
| 230 |
char **ret_prefix, |
| 231 |
uint64_t *ret_left, |
| 232 |
uint64_t *ret_done, |
| 233 |
char **ret_suffix) { |
| 234 |
|
| 235 |
int r; |
| 236 |
|
| 237 |
_cleanup_free_ char *path = NULL; |
| 238 |
r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("LoaderBootCountPath"), &path); |
| 239 |
if (r == -ENOENT) |
| 240 |
return -EUNATCH; /* in this case, let the caller print a message */ |
| 241 |
if (r < 0) |
| 242 |
return log_error_errno(r, "Failed to read LoaderBootCountPath EFI variable: %m"); |
| 243 |
|
| 244 |
if (!path_is_normalized(path)) |
| 245 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 246 |
"Path read from LoaderBootCountPath is not normalized, refusing: %s", |
| 247 |
path); |
| 248 |
|
| 249 |
if (!path_is_absolute(path)) |
| 250 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 251 |
"Path read from LoaderBootCountPath is not absolute, refusing: %s", |
| 252 |
path); |
| 253 |
|
| 254 |
const char *last = NULL; |
| 255 |
r = path_find_last_component(path, /* accept_dot_dot= */ false, /* next= */ NULL, &last); |
| 256 |
if (r < 0) |
| 257 |
return log_error_errno(r, "Failed to extract filename from LoaderBootCountPath '%s': %m", path); |
| 258 |
if (r == 0) |
| 259 |
return log_error_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL), "LoaderBootCountPath '%s' refers to the root directory.", path); |
| 260 |
if (strlen(last) > (size_t) r) |
| 261 |
return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "LoaderBootCountPath '%s' refers to directory path, refusing.", path); |
| 262 |
|
| 263 |
const char *e = strrchr(last, '+'); |
| 264 |
if (!e) |
| 265 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 266 |
"Path read from LoaderBootCountPath does not contain a counter, refusing: %s", |
| 267 |
path); |
| 268 |
|
| 269 |
_cleanup_free_ char *prefix = NULL; |
| 270 |
if (ret_prefix) { |
| 271 |
prefix = strndup(path, e - path); |
| 272 |
if (!prefix) |
| 273 |
return log_oom(); |
| 274 |
} |
| 275 |
|
| 276 |
uint64_t left, done; |
| 277 |
r = parse_counter(path, &e, &left, &done); |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
|
| 281 |
_cleanup_free_ char *suffix = NULL; |
| 282 |
if (ret_suffix) { |
| 283 |
suffix = strdup(e); |
| 284 |
if (!suffix) |
| 285 |
return log_oom(); |
| 286 |
|
| 287 |
*ret_suffix = TAKE_PTR(suffix); |
| 288 |
} |
| 289 |
|
| 290 |
if (ret_path) |
| 291 |
*ret_path = TAKE_PTR(path); |
| 292 |
if (ret_prefix) |
| 293 |
*ret_prefix = TAKE_PTR(prefix); |
| 294 |
if (ret_left) |
| 295 |
*ret_left = left; |
| 296 |
if (ret_done) |
| 297 |
*ret_done = done; |
| 298 |
|
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
static int make_good(const char *prefix, const char *suffix, char **ret) { |
| 303 |
_cleanup_free_ char *good = NULL; |
| 304 |
|
| 305 |
assert(prefix); |
| 306 |
assert(suffix); |
| 307 |
assert(ret); |
| 308 |
|
| 309 |
/* Generate the path we'd use on good boots. This one is easy. If we are successful, we simple drop the counter |
| 310 |
* pair entirely from the name. After all, we know all is good, and the logs will contain information about the |
| 311 |
* tries we needed to come here, hence it's safe to drop the counters from the name. */ |
| 312 |
|
| 313 |
good = strjoin(prefix, suffix); |
| 314 |
if (!good) |
| 315 |
return -ENOMEM; |
| 316 |
|
| 317 |
*ret = TAKE_PTR(good); |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
static int make_bad(const char *prefix, uint64_t done, const char *suffix, char **ret) { |
| 322 |
char *bad; |
| 323 |
|
| 324 |
assert(prefix); |
| 325 |
assert(suffix); |
| 326 |
assert(ret); |
| 327 |
|
| 328 |
/* Generate the path we'd use on bad boots. Let's simply set the 'left' counter to zero, and keep the 'done' |
| 329 |
* counter. The information might be interesting to boot loaders, after all. */ |
| 330 |
|
| 331 |
if (done == 0) |
| 332 |
bad = strjoin(prefix, "+0", suffix); |
| 333 |
else |
| 334 |
bad = asprintf_safe("%s+0-%" PRIu64 "%s", prefix, done, suffix); |
| 335 |
if (!bad) |
| 336 |
return -ENOMEM; |
| 337 |
|
| 338 |
*ret = bad; |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
VERB_DEFAULT_NOARG(verb_status, "status", "Show status of current boot loader entry"); |
| 343 |
static int verb_status(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 344 |
_cleanup_free_ char *path = NULL, *prefix = NULL, *suffix = NULL, *good = NULL, *bad = NULL; |
| 345 |
uint64_t left, done; |
| 346 |
int r; |
| 347 |
|
| 348 |
r = acquire_boot_count_path(&path, &prefix, &left, &done, &suffix); |
| 349 |
if (r == -EUNATCH) { /* No boot count in place, then let's consider this a "clean" boot, |
| 350 |
* since "good", "bad", or "indeterminate" don't apply. */ |
| 351 |
puts("clean"); |
| 352 |
return 0; |
| 353 |
} |
| 354 |
if (r < 0) |
| 355 |
return r; |
| 356 |
|
| 357 |
r = acquire_path(); |
| 358 |
if (r < 0) |
| 359 |
return r; |
| 360 |
|
| 361 |
r = make_good(prefix, suffix, &good); |
| 362 |
if (r < 0) |
| 363 |
return log_oom(); |
| 364 |
|
| 365 |
r = make_bad(prefix, done, suffix, &bad); |
| 366 |
if (r < 0) |
| 367 |
return log_oom(); |
| 368 |
|
| 369 |
log_debug("Booted file: %s\n" |
| 370 |
"The same modified for 'good': %s\n" |
| 371 |
"The same modified for 'bad': %s\n", |
| 372 |
path, |
| 373 |
good, |
| 374 |
bad); |
| 375 |
|
| 376 |
log_debug("Tries left: %" PRIu64"\n" |
| 377 |
"Tries done: %" PRIu64"\n", |
| 378 |
left, done); |
| 379 |
|
| 380 |
STRV_FOREACH(p, arg_path) { |
| 381 |
_cleanup_close_ int fd = -EBADF; |
| 382 |
|
| 383 |
fd = open(*p, O_DIRECTORY|O_CLOEXEC|O_RDONLY); |
| 384 |
if (fd < 0) { |
| 385 |
if (errno == ENOENT) |
| 386 |
continue; |
| 387 |
|
| 388 |
return log_error_errno(errno, "Failed to open $BOOT partition '%s': %m", *p); |
| 389 |
} |
| 390 |
|
| 391 |
if (faccessat(fd, skip_leading_slash(path), F_OK, 0) >= 0) { |
| 392 |
/* If the item we booted with still exists under its name, it means we have not |
| 393 |
* change the current boot's marking so far. This may have two reasons: because we |
| 394 |
* simply didn't do that yet but still plan to, or because the left tries counter is |
| 395 |
* already at zero, hence we cannot further decrease it to mark it even |
| 396 |
* "worse"... Here we check the current counter to detect the latter case and return |
| 397 |
* "dirty", since the item is already marked bad from a previous boot, but otherwise |
| 398 |
* report "indeterminate" since we just didn't make a decision yet. */ |
| 399 |
puts(left == 0 ? "dirty" : "indeterminate"); |
| 400 |
return 0; |
| 401 |
} |
| 402 |
if (errno != ENOENT) |
| 403 |
return log_error_errno(errno, "Failed to check if '%s' exists: %m", path); |
| 404 |
|
| 405 |
if (faccessat(fd, skip_leading_slash(good), F_OK, 0) >= 0) { |
| 406 |
puts("good"); |
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
if (errno != ENOENT) |
| 411 |
return log_error_errno(errno, "Failed to check if '%s' exists: %m", good); |
| 412 |
|
| 413 |
if (faccessat(fd, skip_leading_slash(bad), F_OK, 0) >= 0) { |
| 414 |
puts("bad"); |
| 415 |
return 0; |
| 416 |
} |
| 417 |
if (errno != ENOENT) |
| 418 |
return log_error_errno(errno, "Failed to check if '%s' exists: %m", bad); |
| 419 |
|
| 420 |
/* We didn't find any of the three? If so, let's try the next directory, before we give up. */ |
| 421 |
} |
| 422 |
|
| 423 |
return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Couldn't determine boot state."); |
| 424 |
} |
| 425 |
|
| 426 |
static int rename_in_dir_idempotent(int fd, const char *from, const char *to) { |
| 427 |
int r; |
| 428 |
|
| 429 |
assert(fd >= 0); |
| 430 |
assert(from); |
| 431 |
assert(to); |
| 432 |
|
| 433 |
/* A wrapper around rename_noreplace() which executes no operation if the source and target are the |
| 434 |
* same. */ |
| 435 |
|
| 436 |
if (streq(from, to)) { |
| 437 |
if (faccessat(fd, from, F_OK, AT_SYMLINK_NOFOLLOW) < 0) |
| 438 |
return -errno; |
| 439 |
|
| 440 |
return 0; |
| 441 |
} |
| 442 |
|
| 443 |
r = rename_noreplace(fd, from, fd, to); |
| 444 |
if (r < 0) |
| 445 |
return r; |
| 446 |
|
| 447 |
return 1; |
| 448 |
} |
| 449 |
|
| 450 |
VERB_FULL(verb_set, "good", NULL, VERB_ANY, 1, 0, STATUS_GOOD, |
| 451 |
"Mark this boot as good"); |
| 452 |
VERB_FULL(verb_set, "bad", NULL, VERB_ANY, 1, 0, STATUS_BAD, |
| 453 |
"Mark this boot as bad"); |
| 454 |
VERB_FULL(verb_set, "indeterminate", NULL, VERB_ANY, 1, 0, STATUS_INDETERMINATE, |
| 455 |
"Undo any marking as good or bad"); |
| 456 |
static int verb_set(int argc, char *argv[], uintptr_t data, void *userdata) { |
| 457 |
_cleanup_free_ char *path = NULL, *prefix = NULL, *suffix = NULL, *good = NULL, *bad = NULL; |
| 458 |
const char *target, *source1, *source2; |
| 459 |
uint64_t left, done; |
| 460 |
Status status = data; |
| 461 |
int r; |
| 462 |
|
| 463 |
assert(IN_SET(status, STATUS_GOOD, STATUS_BAD, STATUS_INDETERMINATE)); |
| 464 |
|
| 465 |
r = acquire_boot_count_path(&path, &prefix, &left, &done, &suffix); |
| 466 |
if (r == -EUNATCH) /* acquire_boot_count_path() won't log on its own for this specific error */ |
| 467 |
return log_error_errno(r, "Not booted with boot counting in effect."); |
| 468 |
if (r < 0) |
| 469 |
return r; |
| 470 |
|
| 471 |
r = acquire_path(); |
| 472 |
if (r < 0) |
| 473 |
return r; |
| 474 |
|
| 475 |
r = make_good(prefix, suffix, &good); |
| 476 |
if (r < 0) |
| 477 |
return log_oom(); |
| 478 |
|
| 479 |
r = make_bad(prefix, done, suffix, &bad); |
| 480 |
if (r < 0) |
| 481 |
return log_oom(); |
| 482 |
|
| 483 |
/* Figure out what rename to what */ |
| 484 |
switch (status) { |
| 485 |
case STATUS_GOOD: |
| 486 |
target = good; |
| 487 |
source1 = path; |
| 488 |
source2 = bad; /* Maybe this boot was previously marked as 'bad'? */ |
| 489 |
break; |
| 490 |
case STATUS_BAD: |
| 491 |
target = bad; |
| 492 |
source1 = path; |
| 493 |
source2 = good; /* Maybe this boot was previously marked as 'good'? */ |
| 494 |
break; |
| 495 |
case STATUS_INDETERMINATE: |
| 496 |
if (left == 0) |
| 497 |
return log_error_errno(r, "Current boot entry was already marked bad in a previous boot, cannot reset to indeterminate."); |
| 498 |
|
| 499 |
target = path; |
| 500 |
source1 = good; |
| 501 |
source2 = bad; |
| 502 |
break; |
| 503 |
} |
| 504 |
|
| 505 |
STRV_FOREACH(p, arg_path) { |
| 506 |
_cleanup_close_ int fd = -EBADF; |
| 507 |
|
| 508 |
fd = open(*p, O_DIRECTORY|O_CLOEXEC|O_RDONLY); |
| 509 |
if (fd < 0) |
| 510 |
return log_error_errno(errno, "Failed to open $BOOT partition '%s': %m", *p); |
| 511 |
|
| 512 |
r = rename_in_dir_idempotent(fd, skip_leading_slash(source1), skip_leading_slash(target)); |
| 513 |
if (r == -EEXIST) |
| 514 |
goto exists; |
| 515 |
if (r == -ENOENT) { |
| 516 |
|
| 517 |
r = rename_in_dir_idempotent(fd, skip_leading_slash(source2), skip_leading_slash(target)); |
| 518 |
if (r == -EEXIST) |
| 519 |
goto exists; |
| 520 |
if (r == -ENOENT) { |
| 521 |
|
| 522 |
if (faccessat(fd, skip_leading_slash(target), F_OK, 0) >= 0) /* Hmm, if we can't find either source file, maybe the destination already exists? */ |
| 523 |
goto exists; |
| 524 |
|
| 525 |
if (errno != ENOENT) |
| 526 |
return log_error_errno(errno, "Failed to determine if %s already exists: %m", target); |
| 527 |
|
| 528 |
/* We found none of the snippets here, try the next directory */ |
| 529 |
continue; |
| 530 |
} |
| 531 |
if (r < 0) |
| 532 |
return log_error_errno(r, "Failed to rename '%s' to '%s': %m", source2, target); |
| 533 |
|
| 534 |
if (r > 0) |
| 535 |
log_debug("Successfully renamed '%s' to '%s'.", source2, target); |
| 536 |
else |
| 537 |
log_debug("Not renaming, as '%s' already matches target name.", source2); |
| 538 |
} else if (r < 0) |
| 539 |
return log_error_errno(r, "Failed to rename '%s' to '%s': %m", source1, target); |
| 540 |
else if (r > 0) |
| 541 |
log_debug("Successfully renamed '%s' to '%s'.", source1, target); |
| 542 |
else |
| 543 |
log_debug("Not renaming, as '%s' already matches target name.", source1); |
| 544 |
|
| 545 |
/* First, fsync() the directory these files are located in */ |
| 546 |
r = fsync_parent_at(fd, skip_leading_slash(target)); |
| 547 |
if (r < 0) |
| 548 |
log_debug_errno(r, "Failed to synchronize image directory, ignoring: %m"); |
| 549 |
|
| 550 |
/* Secondly, syncfs() the whole file system these files are located in */ |
| 551 |
if (syncfs(fd) < 0) |
| 552 |
log_debug_errno(errno, "Failed to synchronize $BOOT partition, ignoring: %m"); |
| 553 |
|
| 554 |
log_info("Marked boot as '%s'. (Boot attempt counter is at %" PRIu64".)", argv[0], done); |
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Can't find boot counter source file for '%s'.", target); |
| 559 |
|
| 560 |
exists: |
| 561 |
log_debug("Operation already executed before, not doing anything."); |
| 562 |
return 0; |
| 563 |
} |
| 564 |
|
| 565 |
static int run(int argc, char *argv[]) { |
| 566 |
char **args = NULL; |
| 567 |
int r; |
| 568 |
|
| 569 |
log_setup(); |
| 570 |
|
| 571 |
r = parse_argv(argc, argv, &args); |
| 572 |
if (r <= 0) |
| 573 |
return r; |
| 574 |
|
| 575 |
if (detect_container() > 0) |
| 576 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 577 |
"Marking a boot is not supported in containers."); |
| 578 |
|
| 579 |
if (!is_efi_boot()) |
| 580 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 581 |
"Marking a boot is only supported on EFI systems."); |
| 582 |
|
| 583 |
return dispatch_verb(args, NULL); |
| 584 |
} |
| 585 |
|
| 586 |
DEFINE_MAIN_FUNCTION(run); |