diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-01-25 02:30:02 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-01-25 02:34:24 +0300 |
commit | 9b8f2fb8de9bd2bf1b13a84ab06e394aa9d7cb02 (patch) | |
tree | 316d32604ac502c32e1e6292213164d3dc63328b | |
parent | app.py: don't query user info if no users (diff) | |
download | linux-status-9b8f2fb8de9bd2bf1b13a84ab06e394aa9d7cb02.tar.gz linux-status-9b8f2fb8de9bd2bf1b13a84ab06e394aa9d7cb02.zip |
app.py: don't do systemctl --user if no user instance
-rwxr-xr-x | app.py | 18 |
1 files changed, 17 insertions, 1 deletions
@@ -311,7 +311,10 @@ class UserInstanceStatusTaskList(Task): self.tasks = {user.name: UserInstanceStatusTask.su(user) for user in systemd_users()} else: # As a regular user, we can only query ourselves. - self.tasks = {get_current_user().name: UserInstanceStatusTask()} + self.tasks = {} + user = get_current_user() + if user_instance_active(user): + self.tasks[user.name] = UserInstanceStatusTask() def run(self): for task in self.tasks.values(): @@ -363,6 +366,19 @@ def get_current_user(): return User(entry.pw_uid, entry.pw_name) +def user_instance_active(user): + # I'm pretty sure this is the way to determine if the user instance is + # running? + # Source: https://www.freedesktop.org/software/systemd/man/user@.service.html + unit_name = f'user@{user.uid}.service' + cmd = Systemctl.system('is-active', unit_name, '--quiet') + try: + cmd.run().result() + return True + except Exception: + return False + + # A pitiful attempt to find a list of possibly-systemd-enabled users follows # (i.e. users that might be running a per-user systemd instance). # I don't know of a better way than probing /run/user/UID. |