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

many: support killing running snap apps #14160

Merged
merged 3 commits into from
Jul 25, 2024

Conversation

ZeyadYasser
Copy link
Contributor

@ZeyadYasser ZeyadYasser commented Jul 5, 2024

This is needed in preparation for the new snap remove --terminate flag.

This PR introduces a new /v1/app-control userd endpoint that currently only support killing snap apps. This endpoint will be invoked by snapd during removal of snaps when the --terminate flag is used.

Calling client.AppsKill would do the following for each user session:

  • Find running snap apps transient scope units (matching pattern snap.<snap-name>.*.scope)
  • Send SIGKILL signal to those running snap apps

Looking up snap's transient units and killing them is delegated to systemd by calling systemctl.

This PR is part 3.1 of implementing force removal of snaps according to SD174 - Force removal of snaps spec.

For context:

@ZeyadYasser ZeyadYasser added the Needs Samuele review Needs a review from Samuele before it can land label Jul 5, 2024
@ZeyadYasser ZeyadYasser requested review from bboozzoo and pedronis July 5, 2024 12:15
@ZeyadYasser ZeyadYasser requested review from Meulengracht and zyga and removed request for bboozzoo and Meulengracht July 5, 2024 12:17
Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a quick scroll through the code and most of it looks good.

I have one conceptual question. What happens when after we kill all the apps, the user runs some more. In the grand scheme of things, it seems that some sort of inhibition is needed to be able to kill apps to force remove them and prevent further processes from spawning. I suspect it can be handled with the current removal logic but I wanted to double-check.


func appKill(inst *client.AppInstruction, sysd systemd.Systemd) Response {
// TODO: Use inst.Reason as a hint to explain to users what is happening
if inst.Signal != 9 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, any reason not to use syscall.SIGKILL here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, updated to use syscall.Signal type, Thanks!

@ZeyadYasser
Copy link
Contributor Author

ZeyadYasser commented Jul 10, 2024

it seems that some sort of inhibition is needed to be able to kill apps to force remove them and prevent further processes from spawning. I suspect it can be handled with the current removal logic but I wanted to double-check.

@zyga You are absolutely correct, there is a PR #14126 out for remove inhibition linked in the description. My bad for not making it more obvious.

Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

// for the user session rest api to control apps.
type AppInstruction struct {
Action string `json:"action"`
Snaps []string `json:"snaps,omitempty"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedronis should this stay per-snap or be more specific and be per-app? If we don't worry about running hooks I think we can scope it down to an app list.

We could go even more generic and allow passing a list of security tag patterns?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listing apps would be more consistent with ServiceInstruction, OTOH nothing stops us to add that granularity when it makes sense. For kill this seems the right granularity to me

This method will be used later to list all snap apps'
transient units using the pattern "<snap-name>.*.scope".

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
This helper will be used to find running snap apps
that need to be force killed.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
The new /v1/app-control endpoint currently only supports the "kill"
action. This action finds and kills all running apps for the
requested snaps.

This endpoint will be invoked by snapd during removal of snaps
when the --terminate flag is used.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>

snap: move kill reason to snap package

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>

usersession: use syscall.Signal type for AppInstruction.Signal

Thanks @zyga

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
@ZeyadYasser ZeyadYasser force-pushed the support-killing-running-apps branch from 52fc4ec to ad7e637 Compare July 24, 2024 12:23
@ernestl ernestl merged commit 5f7d584 into canonical:master Jul 25, 2024
46 of 53 checks passed
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Aug 14, 2024
This is an alternative implmentation to the userd approach
in canonical#14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Aug 14, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Aug 23, 2024
This is an alternative implmentation to the userd approach
in canonical#14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>

sandbox/cgroup: add contexts support to FreezeSnapProcesses

Thanks @zyga

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>

sandbox/cgroup: add comment on possible race when killing processes in a v2 cgroup

Thanks @zyga

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>

many: address review comments (thanks @zyga)

- For cgroups V2, only thaw on error
- For cgroups V1, add comment explaining why we always need to thaw
- Update KillSnapProcesses to only send SIGKILL
- Replace deprecated os.IsNotExist with errors.Is

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>

many: address review comments (thanks @zyga)

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Aug 23, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Aug 23, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Aug 23, 2024
This is an alternative implmentation to the userd approach
in canonical#14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

- many: address review comments (thanks @zyga)
  - add contexts support to FreezeSnapProcesses
  - add comment on possible race when killing processes in a v2 cgroup
  - for cgroups V2, only thaw on error
  - for cgroups V1, add comment explaining why we always need to thaw
  - update KillSnapProcesses to only send SIGKILL
  - replace deprecated os.IsNotExist with errors.Is

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Aug 23, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Sep 5, 2024
This is an alternative implmentation to the userd approach
in canonical#14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

- many: address review comments (thanks @zyga)
  - add contexts support to FreezeSnapProcesses
  - add comment on possible race when killing processes in a v2 cgroup
  - for cgroups V2, only thaw on error
  - for cgroups V1, add comment explaining why we always need to thaw
  - update KillSnapProcesses to only send SIGKILL
  - replace deprecated os.IsNotExist with errors.Is

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Sep 5, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Sep 5, 2024
This is an alternative implmentation to the userd approach
in canonical#14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

- many: address review comments (thanks @zyga)
  - add contexts support to FreezeSnapProcesses
  - add comment on possible race when killing processes in a v2 cgroup
  - for cgroups V2, only thaw on error
  - for cgroups V1, add comment explaining why we always need to thaw
  - update KillSnapProcesses to only send SIGKILL
  - replace deprecated os.IsNotExist with errors.Is

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Sep 5, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Sep 10, 2024
This is an alternative implmentation to the userd approach
in canonical#14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

- many: address review comments (thanks @zyga)
  - add contexts support to FreezeSnapProcesses
  - add comment on possible race when killing processes in a v2 cgroup
  - for cgroups V2, only thaw on error
  - for cgroups V1, add comment explaining why we always need to thaw
  - update KillSnapProcesses to only send SIGKILL
  - replace deprecated os.IsNotExist with errors.Is
- trim whitespace when reading freezer.state

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Sep 10, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ZeyadYasser added a commit to ZeyadYasser/snapd that referenced this pull request Sep 10, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ernestl pushed a commit that referenced this pull request Sep 11, 2024
This is an alternative implmentation to the userd approach
in #14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

- many: address review comments (thanks @zyga)
  - add contexts support to FreezeSnapProcesses
  - add comment on possible race when killing processes in a v2 cgroup
  - for cgroups V2, only thaw on error
  - for cgroups V1, add comment explaining why we always need to thaw
  - update KillSnapProcesses to only send SIGKILL
  - replace deprecated os.IsNotExist with errors.Is
- trim whitespace when reading freezer.state

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
ernestl pushed a commit that referenced this pull request Sep 11, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
maykathm pushed a commit to maykathm/snapd that referenced this pull request Sep 11, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
soumyaDghosh pushed a commit to soumyaDghosh/snapd that referenced this pull request Dec 6, 2024
This is an alternative implmentation to the userd approach
in canonical#14160. The userd approach will not work in the absense
of user session daemon (i.e. ubuntu-core) or if a snap is
started as root.

The alternative algorithm kills the running apps in snapd
directly in their cgroups.

- many: address review comments (thanks @zyga)
  - add contexts support to FreezeSnapProcesses
  - add comment on possible race when killing processes in a v2 cgroup
  - for cgroups V2, only thaw on error
  - for cgroups V1, add comment explaining why we always need to thaw
  - update KillSnapProcesses to only send SIGKILL
  - replace deprecated os.IsNotExist with errors.Is
- trim whitespace when reading freezer.state

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
soumyaDghosh pushed a commit to soumyaDghosh/snapd that referenced this pull request Dec 6, 2024
This reverts commit 5f7d584.

Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Samuele review Needs a review from Samuele before it can land
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants