Starting sessions with systemd

https://www.flickr.com/photos/niff/520627425/in/photolist-N1moV-8X9uFQ-N1pCg-VR9FSg-4tGWBK-jFuBU5-mS3D9Z-khe5ZF-VJHPJu-S7oF8f-N1gZh-Hgouj-iewPUs-8WvTqv-tBQPH-8WCjmW-iewxJc-8WvTin-g9L3VE-8WASYG-N1ojv-8WxJyt-8WCjgy-8Ww3it-8WyNCf-8Wz2Pw-8WyFMP-8WyWL3-8Wy3FS-8WyuFJ-8WvsKB-VMBvu3-acH7Kg-8WvW7H-8Wz42o-8WvBH4-8WyEfD-iewDyC-8WvXMn-8Wz6am-numG2F-8WBVJ3-8WvUDg-8Wy2tn-8WCk1Y-8WyyTN-8WvU1V-8Ww1wt-8WyqTs-8WvRtK

I’ve been working on this as a project for a little while now, and it’s getting to a reasonably usable state, so I thought I’d write about it in public.

What?

When you fire up your machine and see GDM’s friendly face smiling back at you, how did it get there? Clearly it was executed, but by what? The short answer is that GDM asked gnome-session to start its UI up for you.

What you see is GNOME Shell started up in a special mode, which it was asked to do by being started in an environment where GNOME_SHELL_SESSION_MODE=gdm. Behind the scenes gnome-session also started a load of session services from the given XDG autostart directories, which gives you gnome-settings-daemon amongst other things. These services can specify at what “phase” they are started at – look for X-GNOME-Autostart-Phase in /etc/xdg/autostart/*.desktop to see this – which gives a form of ordering. The session specifies a bunch of required components, and gnome-session makes sure that these are all running before the session can be considered fully started. If any of them don’t start up, the session has failed to start. Much the same thing exists for the user session too.

Hmm… this is ringing some bells…

Why?

This is quite a crude dependency mechanism. While there are some mechanisms to add extra conditions to autostart services, such as only if a certain file exists or a GSettings key is set, we now (this is not actually a new idea; the bug for this problem was filed in 2012) have a much better way of starting programs when they are needed. That’s systemd of course. If the session needs program X, and program Y needs program X, we can express this. If X isn’t ready until it claims a name on the bus, we can express that too. Restarting things on failure, executing programs at a specified time (alarms), the list goes on.

We already use this stuff for system services in fact, so why not for user ones? If starting a session is essentially executing a bunch of programs at the right time, and we have a utility to execute things in a more flexible way – why not join the two together?

A year or two ago some of us from Ubuntu sat down and worked up an implementation of this for the Unity session we were using back then. We in fact shipped 16.10 (yakkety) with this scheme in place. Since we’re now using GNOME Shell in Ubuntu – and because it’s the right thing to do anyway – it’s time to start working on getting this upstream. Credit goes to Martin Pitt and Sebastien Bacher for the work back then which we’re building on now.

Where are we now?

I’ve got a bunch of patches that I’m starting this week to push to maintainers for review. Most of these are prerequisites for transitioning sessions to be started in this way, but are not harmful when the current method is used either. They can be applied immediately. The actual switch is performed in two places – one to GDM to instruct gnome-session to launch a particular target, and the second to gnome-session to update the session definition files to launch targets instead of executing parts itself.

There is scope for further work here. My initial conversions don’t use particularly advanced features or dependencies. We could probably move more things out of the critical path and have them started on demand.

If you’re worrying about your third party session needing to be reworked for this, don’t be (yet). I’ve been quite careful so far to not require this scheme to be used, so non-GNOME sessions should continue to work as they do now. However, since gnome-session is now duplicating a lot of the same work that systemd is doing, it makes sense for us to talk about how we will go about removing this functionality in a future cycle, so that gnome-session becomes a much smaller thing.

I’ve written a wiki page with a few more technical details. This will be updated as patches are posted and merged.

Hmm, I guess this is kind of lame if you can’t try it. I should make a repository or something.

5 thoughts on “Starting sessions with systemd”

  1. Cool!
    In the end, are there anything gnome-session does that systemd –user can’t do? That is: is gnome-session (in a long-term perspective) needed?

    1. There’s like XDG autostart, the session inhibition stuff, XSMP support.

      Maybe these things can be shipped out to other projects (g-s-d, the shell, …) in the future – not sure, that’s up for discussion. 🙂

  2. What does this mean for people who run GNOME with elogind (there’s no systemd –user there…), and that you’re also planning to remove equivalent support from gnome-session?

    1. I’m not the maintainer of gnome-session, but I think the future will be that this will be the only way that it supports running sessions. Other desktops will want to write units for their components that gnome-session can then start.

Leave a Reply

Your email address will not be published. Required fields are marked *