You are absolutely correct! My apologies for focusing on the greeter session reuse. You are looking for the logic that handles switching to an existing user session, not just the greeter. Let's refine the analysis.
You're right, the SeatManager::switchToGreeter function primarily handles the reuse of greeter sessions, not user sessions. The logic for switching to an existing user session is more nuanced and involves a different part of the code and process.
Here's a more accurate breakdown of the mechanism:
Key Concepts (Refined)
-
Auth Class and Session Start: The Auth class is responsible for the authentication process and the launch of user sessions, which happens after the user successfully logs in via the greeter. It's during this process that SDDM needs to decide whether to start a new user session or switch to an existing one.
-
Login1Manager and Login1Session Interfaces: Again, logind is key. The Auth class uses the Login1Manager interface to get session information and the Login1Session interface to activate existing sessions.
-
Session Type Detection: SDDM needs to identify what kind of session to start (X11, Wayland, or other types) based on the user's preferences and available sessions. The information of which type is used by the user will be within the Session class.
-
Session Activation: If an existing session is found for the user on the current seat and has the same type that the user has selected to log in, SDDM needs to activate it with logind.
Code Analysis (Focusing on User Session Switching)
The relevant code is primarily within the Greeter.cpp and Auth.cpp files and is a bit complex. I'll provide a high level explanation of the process.
Greeter::onSessionStarted & Auth::onSessionStarted: This signal is emitted by Auth when a session has been started (or failed to start). The greeter uses this signal to track if it should continue running, or terminate itself.
Auth::start: The auth class initiates the process of authentication and setting up the session. After receiving the credentials for a user, the Auth class first checks if a display server has been started (through the signal displayServerReady). After that, the code proceeds to check for existing sessions through Login1Manager::ListSessions.
Auth::startSession: This is where the magic happens. The Auth class checks whether there's already a session for the user on the current seat. It iterates through active sessions provided by logind:
- If a user session is found, with a matching session type, it will use the
OrgFreedesktopLogin1SessionInterface to activate the existing session using the Activate() method. Then it signals through the sessionStarted signal that the session start was a success.
- If an existing session isn't found, then
Auth class will proceed to create a new user session.
High-Level Process
- User Authentication: You enter your credentials in the SDDM greeter.
- Authentication and Session Setup: The
Auth class handles the authentication and starts the session creation process.
- Existing Session Check: The
Auth class queries logind for all existing sessions of the user on the current seat, filtering by the session type that is trying to start.
- Session Activation or New Session: If an existing session is found:
- The
Auth class will activate this session. logind will handle unlocking the session, so the user is immediately logged in.
- If no suitable existing session is found, the
Auth class proceeds to launch a new session for the user.
Why Not in SeatManager::switchToGreeter?
The SeatManager::switchToGreeter is about starting the greeter process. The user session login logic takes place after the greeter, when the user has successfully authenticated and is ready to start using their desktop environment. This is why the user session handling is in Auth instead of SeatManager.
Summary
SDDM does not directly handle user session switching in SeatManager::switchToGreeter function. The actual logic for reusing existing user sessions is performed by the Auth class, after a user has successfully logged in through the greeter. The Auth class queries logind for existing user sessions, and if a suitable one exists for the user and the seat, it activates that session instead of creating a new one.
My apologies again for the initial misunderstanding. It's a subtle distinction, but crucial for accurately understanding SDDM's session handling.
Let me know if you would like a deeper dive into the Auth.cpp logic.