220 Commits

Author SHA1 Message Date
Sergio Costas
61be59e81d usersession: Use the app name from .desktop file in notifications (#13678)
* usersession: Use the app name from .desktop file in notifications

Currently, when a notification is shown to the user to inform
they that there is an update for an open snap, the snap name is
used, which can be misleading to the user.

This PR uses the application name from the .desktop file, taking
into account the current locale of the user.

* Add tests for localized name getter

* Take into account the instance key

* Change variable name

* Code simplification

* Try to pass the static analyzer

* Revert "Try to pass the static analyzer"

This reverts commit 3c031bef098d1a1fab6afad02aa456a80fb84154.

* Another try to pass the static analyzer

* Revert "Another try to pass the static analyzer"

This reverts commit 39a37d9e129d1220566e7da9e4be4dd2d3f7324f.

* Added extra tests to check notification text

These four tests ensure that the notification text is correct
when there is an instance key and/or there is a .desktop file
from where a translatable name can be obtained.

* Renamed TestGuessAppIcon... to TestGuessAppData...
2024-05-31 17:10:55 +02:00
Philip Meulengracht
635f5a6a24 usersession/agent: implement support for enable and disable functionality in service code 2024-04-24 15:08:22 +02:00
Philip Meulengracht
521e904982 many: support user-daemons in QueryDisabledServices 2024-04-22 15:05:39 +02:00
Miguel Pires
79c5ac14b2 many: remove usages of deprecated io/ioutil package (#13768)
* many: remove usages of deprecated io/ioutil package

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* .golangci.yml: remove errcheck ignore rule for io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* run-checks: prevent new usages of io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

---------

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2024-04-03 23:23:24 +02:00
Philip Meulengracht
e68b47746f usersession/client: support additional arguments for start/stop/restart operations, 2024-04-03 09:25:55 +02:00
Philip Meulengracht
1bad85906c o/servicestate: support for user services (#13380)
* o/servicestate: support for user services

* o/servicestate: update docstring for easier readability
2024-04-02 14:22:31 +02:00
Sergio Costas
3a571c14f9 agentnotify: show the snap icon when autorefresh is done (#13486)
* agentnotify: show the snap icon when autorefresh is done

When an application has to be closed by the user to allow to do
and autorefresh, the notification has the application icon. But
when the autorefresh has been completed, the notification has
the snapcraft icon instead.

This MR fixes this. With it, the notification specifying that
the snap has been successfully refreshed and that it can be
relaunched shows the application's icon.

Fix https://warthogs.atlassian.net/browse/UDENG-2030

* Fix .desktop file name generation

* Use an heuristic to determine the desktop file

* Move all the logic to the user-space daemon

* Don't fail if ReadCurrentInfo fails

Also, really take the first icon found, and not the last one.

* Moved code into function and added tests

* Update usersession/agent/rest_api.go

Co-authored-by: Maciej Borzecki <maciek.borzecki@gmail.com>

* Update usersession/agent/rest_api.go

Co-authored-by: Maciej Borzecki <maciek.borzecki@gmail.com>

* Extra changes requested by reviewers

* Add extra test

* Extra changes requested

* Update usersession/agent/rest_api_test.go

Co-authored-by: Maciej Borzecki <maciek.borzecki@gmail.com>

* Some style patches

---------

Co-authored-by: Maciej Borzecki <maciek.borzecki@gmail.com>
2024-02-16 08:44:56 +01:00
Philip Meulengracht
c0e89916f4 usersession: implement restart controls and status querying for user services 2024-01-03 09:18:18 +01:00
Alfonso Sánchez-Beato
b4f263bba8 many: replace exec.CombinedOutput when output is parsed
Some invocations to external programs used exec.CombinedOutput, that
combines stdout and strerr into a single byte array. This can be an
issue if this output is parsed, as many programs print debug output or
warnings to stderr and that data is unexpected by the parsers. This
patch changes to using osutil.RunSplitOutput or osutil.RunCmd (that
return separately stdout and stderr) when we need to parse stdout, and
also in some other cases when printing separately both streams could
be helpful. Fixes LP #1885597.
2023-10-05 14:14:28 +02:00
Miguel Pires
29c9752d66 many: s/ioutil.WriteFile/os.WriteFile (#13217)
Replace ioutil.WriteFile with os.WriteFile since the former has been
deprecated since go1.16 and simply calls the latter.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-09-26 11:38:46 +01:00
Oliver Calder
2d35a87cb3 usersession/agent: only close session bus if non-nil
On Ubuntu 16.04, ssh sessions may not have a D-Bus session bus,
resulting in the `bus` field of the session agent being nil.  If this is
the case, calling `s.bus.Close()` results in a nil pointer exception.
This commit adds a check of `s.bus` (like those found elsewhere in
session_agent.go) before calling `s.bus.Close()` during a session
shutdown.

Discovered-by: Andrew Phelps <andrew.phelps@canonical.com>
Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
2023-09-21 08:50:50 +02:00
James Henstridge
6fe532bd07 usersession/autostart: add a test for when desktopentry.Read fails 2023-09-06 16:44:05 +02:00
James Henstridge
b11737bb66 desktop/desktopentry: rename Load to Read 2023-09-06 16:44:05 +02:00
James Henstridge
530d715c2b usersession: port privileged_desktop_launcher code over to desktopentry package 2023-09-06 16:44:05 +02:00
James Henstridge
27e52960bd usersession: migrate autostart package over to using desktopentry 2023-09-06 16:44:05 +02:00
Robert Ancell
0249dab75c agentnotify: Update the text on refresh dialogs 2023-07-31 13:12:37 +02:00
Michael Vogt
0286764631 snapstate: notify user when refresh-app-awareness snap finished (#12715)
* snapstate: notify user when refresh-app-awareness snap refreshed

This is a minimal commit to show a notification when a refresh that
was triggered by the user closing an application is now ready
to use.

* {iface,snap}state: make LinkParticipant pass a *SnapSetup

* snapstate: use notifyLinkParticipant mechanism to trigger finish notification

* agentnotify: move agent notification into their own package

* snapstate: make AutoRefresh() take *AutoRefreshOptions

* agentnotify: import from overlord to ensure linkParticipant handler gets registered

* tests: add new refresh-app-awareness-notify spread test

* snapstate: fix unreachable code

* tests: move from wormhole to test-snapd-sh in refresh-app-awareness-notify

* agentnotify: send notification when snap starts to refresh

* usersession: tweak wording when refresh is ready (thanks to Oliver)

* usersession: actually add the missing finishRefreshNotificationCmd endpoint

* snap: disable graphicalSessionFlow() to avoid confusion

It's unclear if this flow ever worked and the finish-refresh
part for sure never worked because in the userd the API for
finish-notification was not wired in.

This needs to be re-examined when we have a proper UX design.

* usersession: show notification when update is finished (thanks to Oliver)

* agentnotify: remove uneeded notifyUnlinkSnap()

After a quick sync with Oliver the decision was taken to have
just a notification message when the snap refresh is finished
and not when it starts to avoid too many user distractions.

* usersession: tweak wording when refresh is ready (thanks to Oliver)

* agentnotify: remove unneeded comment

* usersession: one more wording tweak (thanks to Oliver)

* agentnotify: add missing unit test

* snapstate: add test that ensures the snapsup.Flags.IsContinuedAutoRefresh is set

* snapstate: add TODO

* many: tweak wordings/comment/variable names (thanks to Miguel)

* tests: simplify refresh-app-awareness-notify test (thanks to Miguel)

* tests: fix quoting in test

* snap: simplify how inhibit worflow inhibited

* tests: tweak tests and ensure "jq" is installed

* agent: fix unit test error
2023-04-18 14:35:02 +02:00
Samuele Pedroni
32f06e8f70 usersession/userd: reminder about issues with XDG_DATA_DIRS modification
with link to past CVE
2023-02-23 13:54:42 +01:00
Miguel Pires
d097436c1c many: fix formatting w/ gofmt 1.19
Go 1.19 includes some changes to gofmt which intend to make lists and
heading clearer when rendered (https://go.dev/doc/go1.19). This commit
is the result of running the new gofmt and manually fixing some of it.
This was necessary because the new gofmt assumed lines beginning w/ tabs
to start lists or examples. While this is often true in our codebase,
we occasionally also use tabs to indent the lines after a TODO or FIXME
prefix or in yaml (e.g., excerpts of a snap.yaml). This meant that a lot of the
reformatted comments were broken and had to be fixed manually.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-01-16 14:23:11 +01:00
Miguel Pires
7e32096d7d Merge pull request #12130 from sergio-costas/remove_duplicate_struct_PendingSnapRefreshInfo
usersession: Remove duplicated struct
2022-09-13 10:24:52 +01:00
Sergio Costas Rodriguez
3697bf35ce usersession: remove duplicated struct
PendingSnapRefreshInfo is alreade defined in the client.go
file, so there is no need to duplicate the definition.
2022-09-10 00:32:25 +02:00
Michael Vogt
d6708bd7e1 daemon,usersession: switch from HeaderMap to Header in tests
The `rec.HeaderMap` is deprecated since go1.11 and the
`rec.Header()` method should be used instead. This commit
does this switch.

Found by https://staticcheck.io/ which was suggested by Fred,
thanks for this.
2022-08-16 20:40:54 +02:00
Fred Lotter
9a8586f5ea systemd: remove the systemctl stop timeout handling
This PR is somewhat related to:
https://github.com/snapcore/snapd/pull/11340

In the process of improving the systemd stop code to better track
the progress of stopping units, we also removed the snapd concept
of a timeout period in order to exclusively rely on
'systemctl stop' to return an error after its internal stop
escalation process failed.

We therefore no longer can produce a 'Timeout' error.

This patch removes code in the code base that used to deal
with this 'Timeout' condition.

The idea behind this patch is that we trust 'systemctl stop'
to tell us if it failed to stop a unit within its configured
timeout, and if it fails, there is nothing more we can do but
to return an error.

Signed-off-by: Fred Lotter <fred.lotter@canonical.com>
2022-05-20 17:45:04 +02:00
Samuele Pedroni
342068c7d4 usersession/agent: killWait was unused, simplify dropping mocking 2022-05-20 17:45:04 +02:00
Frederik Lotter
27b88f8362 systemd: fix snapd systemd-unit stop progress notifications (#11340)
* systemd: fix snapd systemd-unit stop progress notifications

The current behaviour of the systemd stop progress code is to print
the list of systemd units not stopped after 250ms, and then after
every 20 seconds (notice ticker), erroneously print out the same list
even though some of the units may have stopped in the meantime.

However, the bigger issue is that the non-mocked (real) code starts the
stop sequence by making a blocking systemctl stop request and then
afterwords (once all units are stopped) proceed to the progress code,
which has no effect and exits immediately.

The unit tests does not pick this up because the mock systemctl does
not no anything, and therefore the simulation of progress works as
expected because the output of the 'systemctl show' progress calls are
mocked to simulate certain outcomes.

Finally, the 'Stop()' function provided a 'timeout' argument which
suggested the user could control when a stop attempt should timeout
and fail. However, this feature did not work as the original call was
blocking. Furthermore, this features does not make sense because it
creates a second place where timeouts are introduced. The first place
is in the systemd back-end where 'TimeoutStopSec' is optionally
configured by snapd. In contrast with systemd's actual timeout
mechanism described, the 'Stop()' call offered another timeout, but
this mechanism is inconsistently used all over the code-base with
different hard-coded timeout values, not matching the underlying
service defaults to configured values. In the worse case scenario
the snapd 'Stop()' would return an error due to timeout while systemd
is still busy stopping a unit, because it was a longer unit timeout
configure. The timeout value offered by snapd has no real purpose if
carefully analysed.

The following changes were made:

1. Remove the systemd Stop() timeout argument. The timeout is now
   automatically provided by systemdctl when it returns (blocking).
   This allows the timeout to always be aligned with the actual
   timeout 'TimeoutStopSec' provided by the unit, and takes into
   account systemd timeout behaviour related to 'ExecStop' commands.

2. Change the blocking systemctl stop command to async run in a go
   routine allowing progress tracking to continue in parallel. Note
   that the call to Stop() is still blocking because the progress
   code waits until either units are all stopped or the stop command
   returns.

3. Change the notice ticker to rather implement an initial notice
   silence period (1s). After this expires, the first notification
   is generated with the list of units still not stopped. This
   delay was chosen based on empirical tests performed on a
   Raspberry Pi 3 (representing slower targets) under heavy IO and
   CPU load (where it took up to 750ms for 4 services to stop).

4. Change the progress logic to always send a notification after
   the notice silence period on any change to the list of unit
   we are still waiting for, and always only poll the state of the
   remaining units.

5. Add additional unit tests to better cover the new progress and
   notification logic, and make existing tests more thorough.

6. Add critical sections inside the mocked systemctl functions to
   prevent corruption during concurrent modifications to some of
   the test suite logs and counters. This was needed because the
   Stop() is now issued from a go routine.

The following example shows the expected output for 6 systemd units
terminated with the 'Stop' function. The last four (test3 - test6)
has a random post SIGTERM delay before the process exits. The following
notifications are generated for this example after the 1s notification
silence period expires:

Waiting for "test3", "test4", "test5", "test6" to stop.
Waiting for "test3", "test4", "test5" to stop.
Waiting for "test3", "test5" to stop.
Waiting for "test3" to stop.

This patch is required by https://github.com/snapcore/snapd/pull/11113

Signed-off-by: Fred Lotter <fred.lotter@canonical.com>

* typos

* Improved the system stop progress logic.

* Number of small review improvements

* Remove redundant channel write

* reviews: add thread safety into MockSystemctl

* many: have a separate systemd.MockSystemctlWithDelay

keep MockSystemctl signature as before

as we need to introduce controlled delays only in the systemd package
own tests so far

Co-authored-by: Samuele Pedroni <pedronis@lucediurna.net>
2022-05-20 17:29:05 +02:00