MkosiState,
OutputFormat,
Verb,
- current_user_uid_gid,
+ current_user,
flatten,
format_rlimit,
is_dnf_distribution,
cmdline += ["--"]
cmdline += config.cmdline
- uid, _ = current_user_uid_gid()
+ uid = current_user().uid
if config.output_format == OutputFormat.directory:
acl_toggle_remove(config, config.output, uid, allow=False)
def expand_specifier(s: str) -> str:
- user = os.getenv("SUDO_USER") or os.getenv("USER")
- assert user is not None
- return s.replace("%u", user)
+ return s.replace("%u", current_user().name)
def needs_build(config: Union[argparse.Namespace, MkosiConfig]) -> bool:
return list(itertools.chain.from_iterable(lists))
-def current_user_uid_gid() -> tuple[int, int]:
- uid = int(os.getenv("SUDO_UID") or os.getenv("PKEXEC_UID") or os.getuid())
- gid = pwd.getpwuid(uid).pw_gid
- return uid, gid
+@dataclasses.dataclass
+class InvokingUser:
+ _pw: Optional[pwd.struct_passwd] = None
+
+ @classmethod
+ def for_uid(cls, uid: int) -> "InvokingUser":
+ return cls(pwd.getpwuid(uid))
+
+ @property
+ def uid(self) -> int:
+ if self._pw is not None:
+ return self._pw.pw_uid
+ return os.getuid()
+
+ @property
+ def gid(self) -> int:
+ if self._pw is not None:
+ return self._pw.pw_gid
+ return os.getgid()
+
+ @property
+ def name(self) -> str:
+ if self._pw is not None:
+ return self._pw.pw_name
+ return os.getlogin()
+
+ @property
+ def home(self) -> Path:
+ if self._pw is not None:
+ return Path(self._pw.pw_dir)
+ return Path.home()
+
+ def is_running_user(self) -> bool:
+ if self._pw is not None:
+ return self._pw.pw_uid == os.getuid()
+ return True
+
+
+def current_user() -> InvokingUser:
+ uid = os.getenv("SUDO_UID") or os.getenv("PKEXEC_UID")
+ if uid:
+ return InvokingUser.for_uid(int(uid))
+ return InvokingUser()
@contextlib.contextmanager
from types import TracebackType
from typing import Any, Callable, Mapping, Optional, Sequence, Type, TypeVar
-from mkosi.backend import MkosiState, current_user_uid_gid
+from mkosi.backend import MkosiState, current_user
from mkosi.log import ARG_DEBUG, MkosiPrinter, die
from mkosi.types import _FILE, CompletedProcess, PathString, Popen
The function returns the UID-GID pair of the invoking user in the namespace (65436, 65436).
"""
if os.getuid() == 0:
- return current_user_uid_gid()
+ user = current_user()
+ return user.uid, user.gid
+
subuid = read_subrange(Path("/etc/subuid"))
subgid = read_subrange(Path("/etc/subgid"))