Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved check if system is completely booted #384

Merged
merged 1 commit into from
May 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions bin/input-remapper-control
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ def internals(options):
os.system(cmd)


def systemd_finished():
def _num_logged_in_users():
"""Check how many users are logged in."""
who = subprocess.run(['who'], stdout=subprocess.PIPE).stdout.decode()
return len([user for user in who.split('\n') if user.strip() != ""])


def _systemd_finished():
"""Check if systemd finished booting."""
try:
systemd_analyze = subprocess.run(['systemd-analyze'], stdout=subprocess.PIPE)
Expand All @@ -188,6 +194,26 @@ def systemd_finished():
return True

if 'finished' in systemd_analyze.stdout.decode():
# it writes into stderr otherwise or something
return True

return False


def boot_finished():
"""Check if booting is completed."""
# Get as much information as needed to really safely determine if booting up is complete.
# - `who` returns an empty list on some system for security purposes
# - something might be broken and might make systemd_analyze fail:
# Bootup is not yet finished (org.freedesktop.systemd1.Manager.FinishTimestampMonotonic=0).
# Please try again later.
# Hint: Use 'systemctl list-jobs' to see active jobs
if _systemd_finished():
logger.debug('Booting finished')
return True

if _num_logged_in_users() > 0:
logger.debug('User(s) logged in')
return True

return False
Expand All @@ -206,11 +232,11 @@ def main(options):
logger.debug('Call for "%s"', sys.argv)

from inputremapper.configs.paths import USER
boot_finished = systemd_finished()
boot_finished_ = boot_finished()
is_root = USER == "root"
is_autoload = options.command == AUTOLOAD
config_dir_set = options.config_dir is not None
if is_autoload and not boot_finished and is_root and not config_dir_set:
if is_autoload and not boot_finished_ and is_root and not config_dir_set:
# this is probably happening during boot time and got
# triggered by udev. There is no need to try to inject anything if the
# service doesn't know where to look for a config file. This avoids a lot
Expand Down