tree-wide: Get rid of prefix_roota() in favor of path_join()

We deprecated prefix_roota() in favor of chase() and path_join().
Let's finish the removal by replacing the few remaining call sites
with path_join().
This commit is contained in:
Daan De Meyer
2025-05-06 17:40:33 +02:00
parent 38c9ca5380
commit 1fbfbe81b5
14 changed files with 136 additions and 93 deletions
+18 -8
View File
@@ -1091,19 +1091,21 @@ static int setup_volatile_state(const char *directory) {
static int setup_volatile_state_after_remount_idmap(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
_cleanup_free_ char *buf = NULL;
const char *p, *options;
int r;
assert(directory);
/* Then, after remount_idmap(), overmount /var/ with a tmpfs. */
p = prefix_roota(directory, "/var");
_cleanup_free_ char *p = path_join(directory, "/var");
if (!p)
return log_oom();
r = mkdir(p, 0755);
if (r < 0 && errno != EEXIST)
return log_error_errno(errno, "Failed to create %s: %m", directory);
options = "mode=0755" TMPFS_LIMITS_VOLATILE_STATE;
const char *options = "mode=0755" TMPFS_LIMITS_VOLATILE_STATE;
r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
if (r < 0)
return log_oom();
@@ -1116,8 +1118,7 @@ static int setup_volatile_state_after_remount_idmap(const char *directory, uid_t
static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
bool tmpfs_mounted = false, bind_mounted = false;
_cleanup_(rmdir_and_freep) char *template = NULL;
_cleanup_free_ char *buf = NULL, *bindir = NULL;
const char *f, *t, *options;
_cleanup_free_ char *buf = NULL, *bindir = NULL, *f = NULL, *t = NULL;
struct stat st;
int r;
@@ -1148,7 +1149,7 @@ static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char
if (r < 0)
return log_error_errno(r, "Failed to create temporary directory: %m");
options = "mode=0755" TMPFS_LIMITS_ROOTFS;
const char *options = "mode=0755" TMPFS_LIMITS_ROOTFS;
r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
if (r < 0)
goto fail;
@@ -1161,8 +1162,17 @@ static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char
tmpfs_mounted = true;
f = prefix_roota(directory, "/usr");
t = prefix_roota(template, "/usr");
f = path_join(directory, "/usr");
if (!f) {
r = log_oom();
goto fail;
}
t = path_join(template, "/usr");
if (!t) {
r = log_oom();
goto fail;
}
r = mkdir(t, 0755);
if (r < 0 && errno != EEXIST) {
+47 -16
View File
@@ -2260,7 +2260,6 @@ static int make_extra_nodes(const char *dest) {
static int setup_pts(const char *dest, uid_t chown_uid) {
_cleanup_free_ char *options = NULL;
const char *p;
int r;
#if HAVE_SELINUX
@@ -2279,7 +2278,10 @@ static int setup_pts(const char *dest, uid_t chown_uid) {
return log_oom();
/* Mount /dev/pts itself */
p = prefix_roota(dest, "/dev/pts");
_cleanup_free_ char *p = path_join(dest, "/dev/pts");
if (!p)
return log_oom();
r = RET_NERRNO(mkdir(p, 0755));
if (r < 0)
return log_error_errno(r, "Failed to create /dev/pts: %m");
@@ -2292,7 +2294,11 @@ static int setup_pts(const char *dest, uid_t chown_uid) {
return log_error_errno(r, "Failed to chown /dev/pts: %m");
/* Create /dev/ptmx symlink */
p = prefix_roota(dest, "/dev/ptmx");
free(p);
p = path_join(dest, "/dev/ptmx");
if (!p)
return log_oom();
if (symlink("pts/ptmx", p) < 0)
return log_error_errno(errno, "Failed to create /dev/ptmx symlink: %m");
r = userns_lchown(p, 0, 0);
@@ -2300,7 +2306,11 @@ static int setup_pts(const char *dest, uid_t chown_uid) {
return log_error_errno(r, "Failed to chown /dev/ptmx: %m");
/* And fix /dev/pts/ptmx ownership */
p = prefix_roota(dest, "/dev/pts/ptmx");
free(p);
p = path_join(dest, "/dev/pts/ptmx");
if (!p)
return log_oom();
r = userns_lchown(p, 0, 0);
if (r < 0)
return log_error_errno(r, "Failed to chown /dev/pts/ptmx: %m");
@@ -2384,7 +2394,6 @@ int make_run_host(const char *root) {
static int setup_credentials(const char *root) {
bool world_readable = false;
const char *q;
int r;
if (arg_credentials.n_credentials == 0)
@@ -2411,7 +2420,10 @@ static int setup_credentials(const char *root) {
if (r < 0)
return log_error_errno(r, "Failed to create /run/host/credentials: %m");
q = prefix_roota(root, "/run/host/credentials");
_cleanup_free_ char *q = path_join(root, "/run/host/credentials");
if (!q)
return log_oom();
r = mount_nofollow_verbose(LOG_ERR, NULL, q, "ramfs", MS_NOSUID|MS_NOEXEC|MS_NODEV, "mode=0700");
if (r < 0)
return r;
@@ -2527,7 +2539,6 @@ static int setup_hostname(void) {
static int setup_journal(const char *directory) {
_cleanup_free_ char *d = NULL;
const char *p, *q;
sd_id128_t this_id;
bool try;
int r;
@@ -2563,8 +2574,13 @@ static int setup_journal(const char *directory) {
}
}
p = strjoina("/var/log/journal/", SD_ID128_TO_STRING(arg_uuid));
q = prefix_roota(directory, p);
_cleanup_free_ char *p = path_join("/var/log/journal/", SD_ID128_TO_STRING(arg_uuid));
if (!p)
return log_oom();
_cleanup_free_ char *q = path_join(directory, p);
if (!q)
return log_oom();
if (path_is_mount_point(p) > 0) {
if (try)
@@ -2741,7 +2757,6 @@ static int reset_audit_loginuid(void) {
}
static int mount_tunnel_dig(const char *root) {
const char *p, *q;
int r;
if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
@@ -2751,7 +2766,10 @@ static int mount_tunnel_dig(const char *root) {
(void) mkdir_p("/run/systemd/nspawn/", 0755);
(void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
p = strjoina("/run/systemd/nspawn/propagate/", arg_machine);
_cleanup_free_ char *p = path_join("/run/systemd/nspawn/propagate/", arg_machine);
if (!p)
return log_oom();
(void) mkdir_p(p, 0600);
r = make_run_host(root);
@@ -2762,7 +2780,10 @@ static int mount_tunnel_dig(const char *root) {
if (r < 0)
return log_error_errno(r, "Failed to create "NSPAWN_MOUNT_TUNNEL": %m");
q = prefix_roota(root, NSPAWN_MOUNT_TUNNEL);
_cleanup_free_ char *q = path_join(root, NSPAWN_MOUNT_TUNNEL);
if (!q)
return log_oom();
r = mount_nofollow_verbose(LOG_ERR, p, q, NULL, MS_BIND, NULL);
if (r < 0)
return r;
@@ -3819,7 +3840,6 @@ static int outer_child(
_cleanup_(bind_user_context_freep) BindUserContext *bind_user_context = NULL;
_cleanup_strv_free_ char **os_release_pairs = NULL;
bool idmap = false;
const char *p;
pid_t pid;
ssize_t l;
int r;
@@ -4157,7 +4177,10 @@ static int outer_child(
(void) dev_setup(directory, chown_uid, chown_uid);
p = prefix_roota(directory, "/run/host");
_cleanup_free_ char *p = path_join(directory, "/run/host");
if (!p)
return log_oom();
(void) make_inaccessible_nodes(p, chown_uid, chown_uid);
r = setup_unix_export_host_inside(directory, unix_export_path);
@@ -4212,11 +4235,19 @@ static int outer_child(
return r;
/* The same stuff as the $container env var, but nicely readable for the entire payload */
p = prefix_roota(directory, "/run/host/container-manager");
free(p);
p = path_join(directory, "/run/host/container-manager");
if (!p)
return log_oom();
(void) write_string_file(p, arg_container_service_name, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MODE_0444);
/* The same stuff as the $container_uuid env var */
p = prefix_roota(directory, "/run/host/container-uuid");
free(p);
p = path_join(directory, "/run/host/container-uuid");
if (!p)
return log_oom();
(void) write_string_filef(p, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MODE_0444, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(arg_uuid));
if (!arg_use_cgns) {