basic: create new basic/initrd-util.[ch] for initrd-related functions

I changed imports of util.h to initrd-util.h, or added an import of
initrd-util.h, to keep compilation working. It turns out that many files didn't
import util.h directly.

When viewing the patch, don't be confused by git rename detection logic:
a new .c file is added and two functions moved into it.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-11-05 17:29:43 +01:00
parent b61b95ae9e
commit baa6a42d27
32 changed files with 111 additions and 80 deletions

75
src/basic/initrd-util.c Normal file
View file

@ -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;
}

7
src/basic/initrd-util.h Normal file
View file

@ -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);

View file

@ -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',

View file

@ -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) {

View file

@ -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"

View file

@ -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;

View file

@ -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)

View file

@ -5,6 +5,7 @@
#include "efi-loader.h"
#include "generator.h"
#include "initrd-util.h"
#include "log.h"
#include "mkdir.h"
#include "special.h"

View file

@ -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) {

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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)

View file

@ -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"

View file

@ -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"

View file

@ -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;

View file

@ -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"

View file

@ -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;

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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,

View file

@ -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;

View file

@ -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 */

View file

@ -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"

View file

@ -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"

View file

@ -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;

View file

@ -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"