Skip to content

Commit

Permalink
feat(logind)!: configure which session classes to process
Browse files Browse the repository at this point in the history
logind sessions have different classes. So far, all classes of logind
sessions were taken into account. However, this might include greeter
sessions by tools such as lightdm. With this commit, a new config
option for the LogindSessionIdle check allows configuring which classes
of sessions to look at. The default value restricts processing to users
sessions as a sensible default.

BREAKING CHANGE: LogindSessionIdle now only processes sessions of type
    "user" by default. Use the new configuration option classes to also
    include other types in case you need to include them in the checks.
Fixes: #366
  • Loading branch information
languitar committed Aug 13, 2023
1 parent 2958d55 commit 986e558
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/source/available_checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ Options
For instance, ``lingering`` sessions used for background programs might not be of interest.
Default: ``active``, ``online``

.. option:: classes

A comma-separated list of session classes to inspect.
For instance, ``greeter`` sessions used by greeters such as lightdm might not be of interest.
Default: ``user``

Requirements
============

Expand Down
18 changes: 16 additions & 2 deletions src/autosuspend/checks/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,21 @@ def create(
types = [t.strip() for t in types]
states = config.get("states", fallback="active,online").split(",")
states = [t.strip() for t in states]
return cls(name, types, states)
classes = config.get("classes", fallback="user").split(",")
classes = [t.strip() for t in classes]
return cls(name, types, states, classes)

def __init__(self, name: str, types: Iterable[str], states: Iterable[str]) -> None:
def __init__(
self,
name: str,
types: Iterable[str],
states: Iterable[str],
classes: Iterable[str] = ("user"),
) -> None:
Activity.__init__(self, name)
self._types = types
self._states = states
self._classes = classes

@staticmethod
def _list_logind_sessions() -> Iterable[Tuple[str, dict]]:
Expand All @@ -121,6 +130,11 @@ def check(self) -> Optional[str]:
"Ignoring session because its state is %s", properties["State"]
)
continue
if properties["Class"] not in self._classes:
self.logger.debug(
"Ignoring session because its class is %s", properties["Class"]
)
continue

if not properties["IdleHint"]:
return "Login session {} is not idle".format(session_id)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_checks_systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def test_ignore_unknow_type(self, logind: ProxyObject) -> None:
check = LogindSessionsIdle("test", ["not_test"], ["active", "online"])
assert check.check() is None

def test_ignore_unknown_class(self, logind: ProxyObject) -> None:
logind.AddSession("c1", "seat0", 1042, "user", True)

check = LogindSessionsIdle(
"test", ["test"], ["active", "online"], ["nosuchclass"]
)
assert check.check() is None

def test_configure_defaults(self) -> None:
check = LogindSessionsIdle.create("name", config_section())
assert check._types == ["tty", "x11", "wayland"]
Expand All @@ -110,6 +118,12 @@ def test_configure_states(self) -> None:
)
assert check._states == ["test", "bla", "foo"]

def test_configure_classes(self) -> None:
check = LogindSessionsIdle.create(
"name", config_section({"classes": "test, bla,foo"})
)
assert check._classes == ["test", "bla", "foo"]

@pytest.mark.usefixtures("_logind_dbus_error")
def test_dbus_error(self) -> None:
check = LogindSessionsIdle("test", ["test"], ["active", "online"])
Expand Down

0 comments on commit 986e558

Please sign in to comment.