This commit tweaks the in_first_boot() code to make it a proper shared helper. It tweaks the comments about the caching policy. This is important because while this is simialr to `in_initrd()` there is a suble difference: we never cache the result of access() because other parts of systemd will use this file to signal if anything about firstboot changes. This also tweaks it to return an int instead of a bool. So any error is returned as -errno. This allows other parts of systemd (like the random-seed) that need to make decisions about "unknown" first-boot state. This also means that the consumers need to get updated and we can now also use the new helper in random-seed-tool.c and have only a single helper to check for first-boot.
65 lines
1.9 KiB
C
65 lines
1.9 KiB
C
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "env-util.h"
|
|
#include "errno-util.h"
|
|
#include "initrd-util.h"
|
|
#include "log.h"
|
|
|
|
static int saved_in_initrd = -1;
|
|
|
|
bool in_initrd(void) {
|
|
int r;
|
|
|
|
if (saved_in_initrd >= 0)
|
|
return saved_in_initrd;
|
|
|
|
/* If /etc/initrd-release exists, we're in an initrd.
|
|
* This can be overridden by setting SYSTEMD_IN_INITRD=0|1.
|
|
*/
|
|
|
|
r = secure_getenv_bool("SYSTEMD_IN_INITRD");
|
|
if (r < 0 && r != -ENXIO)
|
|
log_debug_errno(r, "Failed to parse $SYSTEMD_IN_INITRD, ignoring: %m");
|
|
|
|
if (r >= 0)
|
|
saved_in_initrd = r > 0;
|
|
else {
|
|
r = RET_NERRNO(access("/etc/initrd-release", F_OK));
|
|
if (r < 0 && r != -ENOENT)
|
|
log_debug_errno(r, "Failed to check if /etc/initrd-release exists, assuming it does not: %m");
|
|
saved_in_initrd = r >= 0;
|
|
}
|
|
|
|
return saved_in_initrd;
|
|
}
|
|
|
|
void in_initrd_force(bool value) {
|
|
saved_in_initrd = value;
|
|
}
|
|
|
|
int in_first_boot(void) {
|
|
static int first_boot_env_parse_cached = -1;
|
|
int r;
|
|
|
|
if (first_boot_env_parse_cached >= 0)
|
|
return first_boot_env_parse_cached;
|
|
|
|
r = secure_getenv_bool("SYSTEMD_FIRST_BOOT");
|
|
if (r >= 0)
|
|
return (first_boot_env_parse_cached = r);
|
|
if (r != -ENXIO)
|
|
log_debug_errno(r, "Failed to parse $SYSTEMD_FIRST_BOOT, ignoring: %m");
|
|
|
|
/* This is not cached and must *never* be cached, other parts of systemd write it to signal
|
|
* an update to the first-boot state. */
|
|
r = RET_NERRNO(access("/run/systemd/first-boot", F_OK));
|
|
if (r >= 0)
|
|
return true;
|
|
if (r == -ENOENT)
|
|
return false;
|
|
|
|
return log_debug_errno(r, "Failed to check /run/systemd/first-boot: %m");
|
|
}
|