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

Implement a background monitor #319

Merged
merged 23 commits into from
May 16, 2019

Conversation

matthiasclasen
Copy link
Contributor

Implement a facility that monitors running flatpaks,
and notifies the user if they run in the background.
Add a permission table to store users responses
persistently.

The platform-dependent parts of this (getting the
state of running applications, and notifying the user)
are handled by a portal backend.

There is currently no application API.

@matthiasclasen
Copy link
Contributor Author

See flatpak/xdg-desktop-portal-gtk#198 for the backend

@matthiasclasen
Copy link
Contributor Author

I've added the background portal to this PR, so applications can now request run-in-background permissions ahead of time.

@hadess
Copy link
Contributor

hadess commented May 14, 2019

I had a quick look through this:

  • the org.freedesktop.portal.Background interface doc doesn't make it clear why it needs a window identifier, and it took me a while to realise that this was to show a dialogue to ask for permission, not for the functionality itself.
  • "Rename access impl" should probably be squashed into the original commit

Is there something that terminates the Flatpak when "running in the background" is disabled and the application is not otherwise running?

src/background.c Outdated Show resolved Hide resolved
@alexlarsson
Copy link
Member

In some earlier permutation of this we had some leeway for being in the background to avoid the "race condition" where an app was just starting in parallel with the app background check. Like, allow the app to be in the background for one cycle, and only ask the second time we check. I think we need something like that to be robust.

src/background.c Outdated Show resolved Hide resolved
@matthiasclasen
Copy link
Contributor Author

* the `org.freedesktop.portal.Background` interface doc doesn't make it clear why it needs a window identifier, and it took me a while to realise that this was to show a dialogue to ask for permission, not for the functionality itself.

I copied the usual docs over now.

* "Rename access impl" should probably be squashed into the original commit

Will do.

Is there something that terminates the Flatpak when "running in the background" is disabled and the application is not otherwise running?

The background monitor checks every few minutes and kills apps that are running in the background and are not allowed. We could probably add a way to trigger that, so the control-center can call it when you change permissions there.

@matthiasclasen matthiasclasen force-pushed the background-monitor branch 3 times, most recently from 6aa4081 to 7420692 Compare May 14, 2019 16:41
@matthiasclasen
Copy link
Contributor Author

In some earlier permutation of this we had some leeway for being in the background to avoid the "race condition" where an app was just starting in parallel with the app background check. Like, allow the app to be in the background for one cycle, and only ask the second time we check. I think we need something like that to be robust.

Still the case. I added a comment to make that clear.

src/background.c Outdated Show resolved Hide resolved
src/background.c Outdated Show resolved Hide resolved
src/background.c Outdated Show resolved Hide resolved
src/background.c Outdated

g_debug ("Killing app %s", app_id);

instance = find_instance (app_id);
Copy link
Member

Choose a reason for hiding this comment

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

What if there are multiple instances? This seems to be a way to escape this. We should kill them all.

@alexlarsson
Copy link
Member

What about commandline apps. I wonder if there is a way to tie an app tty to a terminal and mark that as not backgrounded.

Matthias Clasen added 4 commits May 15, 2019 15:22
Make the NotifyBackground api more flexible
Use xdp_app_info_rewrite_commandline
The configure check for flatpak was a leftover from
when we stopped linking our tests against libflatpak.
We should not bring this back for the main binary.
Instead, ship a standalone copy of FlatpakInstance,
and use that.
@matthiasclasen
Copy link
Contributor Author

I haven't looked at the code more, but one thing that came to mind:

* does the portal remove the autostart symlink when the permission store is updated another way (through the Settings for example)?
* what happens when if the portal isn't running at that time? (eg. do we need to modify the Settings so it goes through the portal to modify that, or at least ensure that it's running)

Currently not. But autostart is not a permission either. Just a file we put in place or remove. If you get autostarted and run in the background without permission for that, you will get killed by the monitor after a little while.

Would it be easier to implement parts of this at the Flatpak level, with the autostart desktop files always being installed but with the .desktop files rewritten so that the background bit only ever get started when the permissions allow it?

We decided to keep flatpak out of it. autostart files are no longer installed by the app, they are created by the portal, and we control what goes in them (ie no conditions, etc). This will make it easier to switch to systemd units later, if we choose to do so.

We might still need to figure out how the session is supposed to know about all this, to shut down background programs that got the permission removed. Maybe @benzea has an idea?

The portal has a monitor that kills flatpaks that are running in the background without the permission.

@matthiasclasen
Copy link
Contributor Author

What about commandline apps. I wonder if there is a way to tie an app tty to a terminal and mark that as not backgrounded.

Haven't thought about that. For now, just giving them the permission to run in the background seems easy enough

Matthias Clasen added 2 commits May 15, 2019 19:17
Redo application tracking
Add an allow-once option
@matthiasclasen
Copy link
Contributor Author

I'm happy with this now.

src/background.c Outdated Show resolved Hide resolved
src/background.c Outdated Show resolved Hide resolved
src/background.c Outdated Show resolved Hide resolved
@alexlarsson
Copy link
Member

I think this looks generally good to go, but the thread issues have to be fixed.

Matthias Clasen added 3 commits May 16, 2019 11:11
Actually make the timeout a minute
better option validation
@alexlarsson
Copy link
Member

Otherwise looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants