nspawn: Simplify mkdir_userns() usage, and trickle that up

One of the things that mkdir_userns{,_p}() does is take an (optional) UID,
and chown the directory to that.  So we need a uid_t argument, and a way of
telling if we should use that uid_t argument.  Fortunately, that is built
in to the uid_t type by having UID_INVALID as a possible value.

However, currently mkdir_userns() also takes a MountSettingsMask and checks
a couple of bits in it to decide if it should perform the chown.

Drop the mask argument, and instead have the caller pass UID_INVALID if it
shouldn't chown.
This commit is contained in:
Luke Shumaker
2018-07-20 12:12:02 -04:00
parent f07b548940
commit 9c0fad5fb5
+6 -9
View File
@@ -442,7 +442,7 @@ int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
}
static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
static int mkdir_userns(const char *path, mode_t mode, uid_t uid_shift) {
int r;
assert(path);
@@ -451,10 +451,7 @@ static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, u
if (r < 0 && r != -EEXIST)
return r;
if ((mask & MOUNT_USE_USERNS) == 0)
return 0;
if (mask & MOUNT_IN_USERNS)
if (uid_shift == UID_INVALID)
return 0;
if (lchown(path, uid_shift, uid_shift) < 0)
@@ -463,7 +460,7 @@ static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, u
return 0;
}
static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, uid_t uid_shift) {
const char *p, *e;
int r;
@@ -490,12 +487,12 @@ static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, Mou
if (prefix && path_startswith(prefix, t))
continue;
r = mkdir_userns(t, mode, mask, uid_shift);
r = mkdir_userns(t, mode, uid_shift);
if (r < 0)
return r;
}
return mkdir_userns(path, mode, mask, uid_shift);
return mkdir_userns(path, mode, uid_shift);
}
int mount_all(const char *dest,
@@ -634,7 +631,7 @@ int mount_all(const char *dest,
if (what && r > 0)
continue;
r = mkdir_userns_p(dest, where, 0755, mount_settings, uid_shift);
r = mkdir_userns_p(dest, where, 0755, (use_userns && !in_userns) ? uid_shift : UID_INVALID);
if (r < 0 && r != -EEXIST) {
if (fatal && r != -EROFS)
return log_error_errno(r, "Failed to create directory %s: %m", where);