diff options
32 files changed, 111 insertions, 80 deletions
diff --git a/src/basic/initrd-util.c b/src/basic/initrd-util.c new file mode 100644 index 0000000000..2b6809aea5 --- /dev/null +++ b/src/basic/initrd-util.c @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include <unistd.h> + +#include "env-util.h" +#include "initrd-util.h" +#include "parse-util.h" +#include "stat-util.h" +#include "string-util.h" + +static int saved_in_initrd = -1; + +bool in_initrd(void) { + int r; + const char *e; + bool lenient = false; + + if (saved_in_initrd >= 0) + return saved_in_initrd; + + /* We have two checks here: + * + * 1. the flag file /etc/initrd-release must exist + * 2. the root file system must be a memory file system + * + * The second check is extra paranoia, since misdetecting an + * initrd can have bad consequences due the initrd + * emptying when transititioning to the main systemd. + * + * If env var $SYSTEMD_IN_INITRD is not set or set to "auto", + * both checks are used. If it's set to "lenient", only check + * 1 is used. If set to a boolean value, then the boolean + * value is returned. + */ + + e = secure_getenv("SYSTEMD_IN_INITRD"); + if (e) { + if (streq(e, "lenient")) + lenient = true; + else if (!streq(e, "auto")) { + r = parse_boolean(e); + if (r >= 0) { + saved_in_initrd = r > 0; + return saved_in_initrd; + } + log_debug_errno(r, "Failed to parse $SYSTEMD_IN_INITRD, ignoring: %m"); + } + } + + if (!lenient) { + r = path_is_temporary_fs("/"); + if (r < 0) + log_debug_errno(r, "Couldn't determine if / is a temporary file system: %m"); + + saved_in_initrd = r > 0; + } + + r = access("/etc/initrd-release", F_OK); + if (r >= 0) { + if (saved_in_initrd == 0) + log_debug("/etc/initrd-release exists, but it's not an initrd."); + else + saved_in_initrd = 1; + } else { + if (errno != ENOENT) + log_debug_errno(errno, "Failed to test if /etc/initrd-release exists: %m"); + saved_in_initrd = 0; + } + + return saved_in_initrd; +} + +void in_initrd_force(bool value) { + saved_in_initrd = value; +} diff --git a/src/basic/initrd-util.h b/src/basic/initrd-util.h new file mode 100644 index 0000000000..173093c069 --- /dev/null +++ b/src/basic/initrd-util.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include <stdbool.h> + +bool in_initrd(void); +void in_initrd_force(bool value); diff --git a/src/basic/meson.build b/src/basic/meson.build index bfe52d5879..c1d970b6a9 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -80,6 +80,8 @@ basic_sources = files( 'hostname-util.h', 'in-addr-util.c', 'in-addr-util.h', + 'initrd-util.c', + 'initrd-util.h', 'inotify-util.c', 'inotify-util.h', 'io-util.c', diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c index 410b8a3eb5..eea70d8606 100644 --- a/src/basic/proc-cmdline.c +++ b/src/basic/proc-cmdline.c @@ -7,13 +7,13 @@ #include "efivars.h" #include "extract-word.h" #include "fileio.h" +#include "initrd-util.h" #include "macro.h" #include "parse-util.h" #include "proc-cmdline.h" #include "process-util.h" #include "special.h" #include "string-util.h" -#include "util.h" #include "virt.h" int proc_cmdline(char **ret) { diff --git a/src/basic/unit-file.c b/src/basic/unit-file.c index c81c69db30..1334365c2f 100644 --- a/src/basic/unit-file.c +++ b/src/basic/unit-file.c @@ -6,6 +6,7 @@ #include "dirent-util.h" #include "fd-util.h" #include "fs-util.h" +#include "initrd-util.h" #include "macro.h" #include "path-lookup.h" #include "set.h" diff --git a/src/basic/util.c b/src/basic/util.c index e1f090ebb9..c47ea4584b 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -7,7 +7,6 @@ #include "alloc-util.h" #include "build.h" #include "env-file.h" -#include "env-util.h" #include "fd-util.h" #include "fileio.h" #include "hostname-util.h" @@ -21,71 +20,6 @@ int saved_argc = 0; char **saved_argv = NULL; -static int saved_in_initrd = -1; - -bool in_initrd(void) { - int r; - const char *e; - bool lenient = false; - - if (saved_in_initrd >= 0) - return saved_in_initrd; - - /* We have two checks here: - * - * 1. the flag file /etc/initrd-release must exist - * 2. the root file system must be a memory file system - * - * The second check is extra paranoia, since misdetecting an - * initrd can have bad consequences due the initrd - * emptying when transititioning to the main systemd. - * - * If env var $SYSTEMD_IN_INITRD is not set or set to "auto", - * both checks are used. If it's set to "lenient", only check - * 1 is used. If set to a boolean value, then the boolean - * value is returned. - */ - - e = secure_getenv("SYSTEMD_IN_INITRD"); - if (e) { - if (streq(e, "lenient")) - lenient = true; - else if (!streq(e, "auto")) { - r = parse_boolean(e); - if (r >= 0) { - saved_in_initrd = r > 0; - return saved_in_initrd; - } - log_debug_errno(r, "Failed to parse $SYSTEMD_IN_INITRD, ignoring: %m"); - } - } - - if (!lenient) { - r = path_is_temporary_fs("/"); - if (r < 0) - log_debug_errno(r, "Couldn't determine if / is a temporary file system: %m"); - - saved_in_initrd = r > 0; - } - - r = access("/etc/initrd-release", F_OK); - if (r >= 0) { - if (saved_in_initrd == 0) - log_debug("/etc/initrd-release exists, but it's not an initrd."); - else - saved_in_initrd = 1; - } else { - if (errno != ENOENT) - log_debug_errno(errno, "Failed to test if /etc/initrd-release exists: %m"); - saved_in_initrd = 0; - } - - return saved_in_initrd; -} - -void in_initrd_force(bool value) { - saved_in_initrd = value; -} int container_get_leader(const char *machine, pid_t *pid) { _cleanup_free_ char *s = NULL, *class = NULL; diff --git a/src/basic/util.h b/src/basic/util.h index 0fcc1882f6..347e4fe4b0 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -19,9 +19,6 @@ static inline void save_argc_argv(int argc, char **argv) { saved_argv = argv; } -bool in_initrd(void); -void in_initrd_force(bool value); - /* Note: log2(0) == log2(1) == 0 here and below. */ #define CONST_LOG2ULL(x) ((x) > 1 ? (unsigned) __builtin_clzll(x) ^ 63U : 0) diff --git a/src/boot/bless-boot-generator.c b/src/boot/bless-boot-generator.c index 6adef5b3dc..5120b9622e 100644 --- a/src/boot/bless-boot-generator.c +++ b/src/boot/bless-boot-generator.c @@ -5,6 +5,7 @@ #include "efi-loader.h" #include "generator.h" +#include "initrd-util.h" #include "log.h" #include "mkdir.h" #include "special.h" diff --git a/src/core/bpf-util.c b/src/core/bpf-util.c index 84170da0a8..6fe229e32d 100644 --- a/src/core/bpf-util.c +++ b/src/core/bpf-util.c @@ -3,6 +3,7 @@ #include "bpf-dlopen.h" #include "bpf-util.h" #include "cgroup-util.h" +#include "initrd-util.h" #include "log.h" bool cgroup_bpf_supported(void) { diff --git a/src/core/import-creds.c b/src/core/import-creds.c index 4685e43f47..1f2017ee5a 100644 --- a/src/core/import-creds.c +++ b/src/core/import-creds.c @@ -9,6 +9,7 @@ #include "format-util.h" #include "fs-util.h" #include "hexdecoct.h" +#include "initrd-util.h" #include "import-creds.h" #include "io-util.h" #include "mkdir-label.h" diff --git a/src/core/main.c b/src/core/main.c index 14a4f81452..672345368c 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -52,6 +52,7 @@ #include "hostname-setup.h" #include "ima-setup.h" #include "import-creds.h" +#include "initrd-util.h" #include "killall.h" #include "kmod-setup.h" #include "limits-util.h" @@ -92,7 +93,6 @@ #include "time-util.h" #include "umask-util.h" #include "user-util.h" -#include "util.h" #include "virt.h" #include "watchdog.h" diff --git a/src/core/manager-serialize.c b/src/core/manager-serialize.c index 27cb0925ae..dd16d17afa 100644 --- a/src/core/manager-serialize.c +++ b/src/core/manager-serialize.c @@ -6,6 +6,7 @@ #include "fd-util.h" #include "fileio.h" #include "format-util.h" +#include "initrd-util.h" #include "macro.h" #include "manager-serialize.h" #include "manager.h" diff --git a/src/core/manager.c b/src/core/manager.c index a59afafb58..1473425e4e 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -48,6 +48,7 @@ #include "fileio.h" #include "generator-setup.h" #include "hashmap.h" +#include "initrd-util.h" #include "inotify-util.h" #include "install.h" #include "io-util.h" diff --git a/src/core/mount.c b/src/core/mount.c index 5e8a6ead61..be46e56689 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -14,6 +14,7 @@ #include "exit-status.h" #include "format-util.h" #include "fstab-util.h" +#include "initrd-util.h" #include "libmount-util.h" #include "log.h" #include "manager.h" diff --git a/src/core/selinux-setup.c b/src/core/selinux-setup.c index 3f873baa91..153322442c 100644 --- a/src/core/selinux-setup.c +++ b/src/core/selinux-setup.c @@ -8,13 +8,13 @@ #include <selinux/selinux.h> #endif +#include "initrd-util.h" #include "log.h" #include "macro.h" #include "selinux-setup.h" #include "selinux-util.h" #include "string-util.h" #include "time-util.h" -#include "util.h" #if HAVE_SELINUX _printf_(2,3) diff --git a/src/debug-generator/debug-generator.c b/src/debug-generator/debug-generator.c index cf638fcc56..1fe2b56810 100644 --- a/src/debug-generator/debug-generator.c +++ b/src/debug-generator/debug-generator.c @@ -5,6 +5,7 @@ #include "alloc-util.h" #include "dropin.h" #include "generator.h" +#include "initrd-util.h" #include "mkdir-label.h" #include "parse-util.h" #include "path-util.h" diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index e76de45a0f..872e01f58f 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -13,6 +13,7 @@ #include "fstab-util.h" #include "generator.h" #include "in-addr-util.h" +#include "initrd-util.h" #include "log.h" #include "main-func.h" #include "mkdir.h" @@ -29,7 +30,6 @@ #include "string-util.h" #include "strv.h" #include "unit-name.h" -#include "util.h" #include "virt.h" #include "volatile-util.h" diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c index 0fb53bb9ea..f939b2e8c2 100644 --- a/src/gpt-auto-generator/gpt-auto-generator.c +++ b/src/gpt-auto-generator/gpt-auto-generator.c @@ -23,6 +23,7 @@ #include "fstab-util.h" #include "generator.h" #include "gpt.h" +#include "initrd-util.h" #include "mkdir.h" #include "mountpoint-util.h" #include "parse-util.h" @@ -34,7 +35,6 @@ #include "string-util.h" #include "strv.h" #include "unit-name.h" -#include "util.h" #include "virt.h" static const char *arg_dest = NULL; diff --git a/src/hibernate-resume/hibernate-resume-generator.c b/src/hibernate-resume/hibernate-resume-generator.c index 82c2a56493..be52c21d00 100644 --- a/src/hibernate-resume/hibernate-resume-generator.c +++ b/src/hibernate-resume/hibernate-resume-generator.c @@ -8,6 +8,7 @@ #include "dropin.h" #include "fstab-util.h" #include "generator.h" +#include "initrd-util.h" #include "log.h" #include "main-func.h" #include "mkdir-label.h" diff --git a/src/hibernate-resume/hibernate-resume.c b/src/hibernate-resume/hibernate-resume.c index 1c7d9179d8..9a9df5d22f 100644 --- a/src/hibernate-resume/hibernate-resume.c +++ b/src/hibernate-resume/hibernate-resume.c @@ -7,8 +7,8 @@ #include "alloc-util.h" #include "devnum-util.h" #include "fileio.h" +#include "initrd-util.h" #include "log.h" -#include "util.h" int main(int argc, char *argv[]) { struct stat st; diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 71d7a59bda..c02d73bdc2 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -27,6 +27,7 @@ #include "hashmap.h" #include "hostname-util.h" #include "id128-util.h" +#include "initrd-util.h" #include "io-util.h" #include "journal-authenticate.h" #include "journal-internal.h" diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 684d635d36..2db8bb6074 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -26,6 +26,7 @@ #include "fileio.h" #include "firewall-util.h" #include "fs-util.h" +#include "initrd-util.h" #include "local-addresses.h" #include "netlink-util.h" #include "network-internal.h" diff --git a/src/partition/repart.c b/src/partition/repart.c index 8e3cfece89..44060c9413 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -40,6 +40,7 @@ #include "hexdecoct.h" #include "hmac.h" #include "id128-util.h" +#include "initrd-util.h" #include "io-util.h" #include "json.h" #include "list.h" diff --git a/src/shared/condition.c b/src/shared/condition.c index a23d6a3e45..f404d99878 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -34,6 +34,7 @@ #include "fs-util.h" #include "glob-util.h" #include "hostname-util.h" +#include "initrd-util.h" #include "ima-util.h" #include "limits-util.h" #include "list.h" diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index f683f05981..3cbc6be248 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -7,6 +7,7 @@ #include "alloc-util.h" #include "device-nodes.h" #include "fstab-util.h" +#include "initrd-util.h" #include "macro.h" #include "mount-util.h" #include "nulstr-util.h" diff --git a/src/shared/generator.c b/src/shared/generator.c index 5d019f4f4e..7eed3b76c1 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -11,6 +11,7 @@ #include "fileio.h" #include "fstab-util.h" #include "generator.h" +#include "initrd-util.h" #include "log.h" #include "macro.h" #include "mkdir-label.h" @@ -21,7 +22,6 @@ #include "string-util.h" #include "time-util.h" #include "unit-name.h" -#include "util.h" int generator_open_unit_file( const char *dir, diff --git a/src/shared/killall.c b/src/shared/killall.c index 35180870a8..133b0e28d6 100644 --- a/src/shared/killall.c +++ b/src/shared/killall.c @@ -13,6 +13,7 @@ #include "dirent-util.h" #include "fd-util.h" #include "format-util.h" +#include "initrd-util.h" #include "killall.h" #include "parse-util.h" #include "process-util.h" @@ -20,7 +21,6 @@ #include "stdio-util.h" #include "string-util.h" #include "terminal-util.h" -#include "util.h" static bool ignore_proc(pid_t pid, bool warn_rootfs) { _cleanup_fclose_ FILE *f = NULL; diff --git a/src/shared/switch-root.c b/src/shared/switch-root.c index 1a444841fa..0b93312bbf 100644 --- a/src/shared/switch-root.c +++ b/src/shared/switch-root.c @@ -11,6 +11,7 @@ #include "base-filesystem.h" #include "chase-symlinks.h" #include "fd-util.h" +#include "initrd-util.h" #include "log.h" #include "missing_syscall.h" #include "mkdir-label.h" @@ -23,7 +24,6 @@ #include "strv.h" #include "switch-root.h" #include "user-util.h" -#include "util.h" int switch_root(const char *new_root, const char *old_root_after, /* path below the new root, where to place the old root after the transition */ diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c index dcee0f9006..045ff9f418 100644 --- a/src/shutdown/shutdown.c +++ b/src/shutdown/shutdown.c @@ -24,6 +24,7 @@ #include "exec-util.h" #include "fd-util.h" #include "fileio.h" +#include "initrd-util.h" #include "killall.h" #include "log.h" #include "parse-util.h" @@ -36,7 +37,6 @@ #include "sysctl-util.h" #include "terminal-util.h" #include "umount.h" -#include "util.h" #include "virt.h" #include "watchdog.h" diff --git a/src/sysext/sysext.c b/src/sysext/sysext.c index 0875099d5f..839e2f92b8 100644 --- a/src/sysext/sysext.c +++ b/src/sysext/sysext.c @@ -20,6 +20,7 @@ #include "format-table.h" #include "fs-util.h" #include "hashmap.h" +#include "initrd-util.h" #include "log.h" #include "main-func.h" #include "missing_magic.h" diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index 1f43bb3eb0..1d54066ae6 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -3,13 +3,13 @@ #include "alloc-util.h" #include "env-util.h" #include "errno-util.h" +#include "initrd-util.h" #include "log.h" #include "macro.h" #include "proc-cmdline.h" #include "special.h" #include "string-util.h" #include "tests.h" -#include "util.h" static int obj; diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index dffa2822e6..27a73d61a0 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "initrd-util.h" #include "path-lookup.h" #include "set.h" #include "special.h" |