]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Support Wayland sessions / greeters
[sojka/lightdm.git] / src / seat.c
1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/wait.h>
15
16 #include "seat.h"
17 #include "configuration.h"
18 #include "guest-account.h"
19 #include "greeter.h"
20 #include "session-config.h"
21
22 enum {
23     SESSION_ADDED,
24     RUNNING_USER_SESSION,
25     SESSION_REMOVED,
26     STOPPED,
27     LAST_SIGNAL
28 };
29 static guint signals[LAST_SIGNAL] = { 0 };
30
31 struct SeatPrivate
32 {
33     /* XDG name for this seat */
34     gchar *name;
35
36     /* Configuration for this seat */
37     GHashTable *properties;
38
39     /* TRUE if this seat can run multiple sessions at once */
40     gboolean supports_multi_session;
41
42     /* TRUE if display server can be shared for sessions */
43     gboolean share_display_server;
44
45     /* The display servers on this seat */
46     GList *display_servers;
47
48     /* The sessions on this seat */
49     GList *sessions;
50
51     /* The last session set to active */
52     Session *active_session;
53
54     /* The session belonging to the active greeter user */
55     Session *next_session;
56
57     /* The session to set active when it starts */
58     Session *session_to_activate;
59
60     /* TRUE once we have started */
61     gboolean started;
62
63     /* TRUE if stopping this seat (waiting for displays to stop) */
64     gboolean stopping;
65
66     /* TRUE if stopped */
67     gboolean stopped;
68 };
69
70 static void seat_logger_iface_init (LoggerInterface *iface);
71
72 G_DEFINE_TYPE_WITH_CODE (Seat, seat, G_TYPE_OBJECT,
73                          G_IMPLEMENT_INTERFACE (
74                              LOGGER_TYPE, seat_logger_iface_init));
75
76 typedef struct
77 {
78     const gchar *name;
79     GType type;
80 } SeatModule;
81 static GHashTable *seat_modules = NULL;
82
83 // FIXME: Make a get_display_server() that re-uses display servers if supported
84 static DisplayServer *create_display_server (Seat *seat, Session *session);
85 static Greeter *create_greeter_session (Seat *seat);
86 static void start_session (Seat *seat, Session *session);
87
88 void
89 seat_register_module (const gchar *name, GType type)
90 {
91     SeatModule *module;
92
93     if (!seat_modules)
94         seat_modules = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
95
96     g_debug ("Registered seat module %s", name);
97
98     module = g_malloc0 (sizeof (SeatModule));
99     module->name = g_strdup (name);
100     module->type = type;
101     g_hash_table_insert (seat_modules, g_strdup (name), module);
102 }
103
104 Seat *
105 seat_new (const gchar *module_name, const gchar *name)
106 {
107     Seat *seat;
108     SeatModule *m = NULL;
109
110     g_return_val_if_fail (module_name != NULL, NULL);
111
112     if (seat_modules)
113         m = g_hash_table_lookup (seat_modules, module_name);
114     if (!m)
115         return NULL;
116
117     seat = g_object_new (m->type, NULL);
118     seat->priv->name = g_strdup (name);
119
120     return seat;
121 }
122
123 void
124 seat_set_property (Seat *seat, const gchar *name, const gchar *value)
125 {
126     g_return_if_fail (seat != NULL);
127     g_hash_table_insert (seat->priv->properties, g_strdup (name), g_strdup (value));
128 }
129
130 const gchar *
131 seat_get_string_property (Seat *seat, const gchar *name)
132 {
133     g_return_val_if_fail (seat != NULL, NULL);
134     return g_hash_table_lookup (seat->priv->properties, name);
135 }
136
137 gchar **
138 seat_get_string_list_property (Seat *seat, const gchar *name)
139 {
140     g_return_val_if_fail (seat != NULL, NULL);
141     return g_strsplit (g_hash_table_lookup (seat->priv->properties, name), ";", 0);
142 }
143
144 gboolean
145 seat_get_boolean_property (Seat *seat, const gchar *name)
146 {
147     return g_strcmp0 (seat_get_string_property (seat, name), "true") == 0;
148 }
149
150 gint
151 seat_get_integer_property (Seat *seat, const gchar *name)
152 {
153     const gchar *value;
154
155     value = seat_get_string_property (seat, name);
156     return value ? atoi (value) : 0;
157 }
158
159 const gchar *
160 seat_get_name (Seat *seat)
161 {
162     return seat->priv->name;
163 }
164
165 void
166 seat_set_supports_multi_session (Seat *seat, gboolean supports_multi_session)
167 {
168     g_return_if_fail (seat != NULL);
169     seat->priv->supports_multi_session = supports_multi_session;
170 }
171
172 void
173 seat_set_share_display_server (Seat *seat, gboolean share_display_server)
174 {
175     g_return_if_fail (seat != NULL);
176     seat->priv->share_display_server = share_display_server;
177 }
178
179 gboolean
180 seat_start (Seat *seat)
181 {
182     g_return_val_if_fail (seat != NULL, FALSE);
183
184     l_debug (seat, "Starting");
185
186     SEAT_GET_CLASS (seat)->setup (seat);
187     seat->priv->started = SEAT_GET_CLASS (seat)->start (seat);
188
189     return seat->priv->started;
190 }
191
192 GList *
193 seat_get_sessions (Seat *seat)
194 {
195     g_return_val_if_fail (seat != NULL, NULL);
196     return seat->priv->sessions;
197 }
198
199 static gboolean
200 set_greeter_idle (gpointer greeter)
201 {
202     greeter_idle (GREETER (greeter));
203     return FALSE;
204 }
205
206 void
207 seat_set_active_session (Seat *seat, Session *session)
208 {
209     GList *link;
210
211     g_return_if_fail (seat != NULL);
212
213     SEAT_GET_CLASS (seat)->set_active_session (seat, session);
214
215     /* Stop any greeters */
216     for (link = seat->priv->sessions; link; link = link->next)
217     {
218         Session *s = link->data;
219
220         if (s == session || session_get_is_stopping (s))
221             continue;
222
223         if (IS_GREETER (s))
224         {
225             Greeter *greeter = GREETER (s);
226             if (greeter_get_resettable (greeter))
227             {
228                 if (seat->priv->active_session == SESSION (greeter))
229                 {
230                     l_debug (seat, "Idling greeter");
231                     /* Do this in an idle callback, because we might very well
232                        be in the middle of responding to a START_SESSION
233                        request by a greeter.  So they won't expect an IDLE
234                        call during that.  Plus, this isn't time-sensitive. */
235                     g_idle_add (set_greeter_idle, greeter);
236                 }
237             }
238             else
239             {
240                 l_debug (seat, "Stopping greeter");
241                 session_stop (s);
242             }
243         }
244     }
245
246     /* Lock previous sessions */
247     if (seat->priv->active_session)
248     {
249         if (session != seat->priv->active_session && !IS_GREETER (seat->priv->active_session))
250             session_lock (seat->priv->active_session);
251         g_object_unref (seat->priv->active_session);
252     }
253
254     session_activate (session);
255     seat->priv->active_session = g_object_ref (session);
256 }
257
258 Session *
259 seat_get_active_session (Seat *seat)
260 {
261     g_return_val_if_fail (seat != NULL, NULL);
262     return SEAT_GET_CLASS (seat)->get_active_session (seat);
263 }
264
265 Session *
266 seat_get_next_session (Seat *seat)
267 {
268     g_return_val_if_fail (seat != NULL, NULL);
269     return seat->priv->next_session;
270 }
271
272 /**
273  * Obtains the active session which lightdm expects to be active.
274  *
275  * This function is different from seat_get_active_session() in that the
276  * later (in the case of xlocal seats) dynamically finds the session that is
277  * really active (based on the active VT), whereas this function returns the
278  * session that lightdm activated last by itself, which may not be the actual
279  * active session (i.e. VT changes).
280  */
281 Session *
282 seat_get_expected_active_session (Seat *seat)
283 {
284     g_return_val_if_fail (seat != NULL, NULL);
285     return seat->priv->active_session;
286 }
287
288 /**
289  * Sets the active session which lightdm expects to be active.
290  *
291  * This function is different from seat_set_active_session() in that the
292  * later performs an actual session activation, whereas this function just
293  * updates the active session after the session has been activated by some
294  * means external to lightdm (i.e. VT changes).
295  */
296 void
297 seat_set_externally_activated_session (Seat *seat, Session *session)
298 {
299     g_return_if_fail (seat != NULL);
300     if (seat->priv->active_session)
301         g_object_unref (seat->priv->active_session);
302     seat->priv->active_session = g_object_ref (session);
303 }
304
305 Session *
306 seat_find_session_by_login1_id (Seat *seat, const gchar *login1_session_id)
307 {
308     GList *session_link;
309
310     for (session_link = seat->priv->sessions; session_link; session_link = session_link->next)
311     {
312         Session *session = session_link->data;
313         if (g_strcmp0 (login1_session_id, session_get_login1_session_id (session)) == 0)
314             return session;
315     }
316
317     return NULL;
318 }
319
320 gboolean
321 seat_get_can_switch (Seat *seat)
322 {
323     g_return_val_if_fail (seat != NULL, FALSE);
324     return seat_get_boolean_property (seat, "allow-user-switching") && seat->priv->supports_multi_session;
325 }
326
327 gboolean
328 seat_get_allow_guest (Seat *seat)
329 {
330     g_return_val_if_fail (seat != NULL, FALSE);
331     return seat_get_boolean_property (seat, "allow-guest") && guest_account_is_installed ();
332 }
333
334 static gboolean
335 run_script (Seat *seat, DisplayServer *display_server, const gchar *script_name, User *user)
336 {
337     Process *script;
338     gboolean result = FALSE;
339
340     script = process_new (NULL, NULL);
341
342     process_set_command (script, script_name);
343
344     /* Set POSIX variables */
345     process_set_clear_environment (script, TRUE);
346     process_set_env (script, "SHELL", "/bin/sh");
347
348     /* Variables required for regression tests */
349     if (g_getenv ("LIGHTDM_TEST_ROOT"))
350     {
351         process_set_env (script, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
352         process_set_env (script, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
353         process_set_env (script, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
354         process_set_env (script, "PATH", g_getenv ("PATH"));
355     }
356     else
357         process_set_env (script, "PATH", "/usr/local/bin:/usr/bin:/bin");
358
359     if (user)
360     {
361         process_set_env (script, "USER", user_get_name (user));
362         process_set_env (script, "LOGNAME", user_get_name (user));
363         process_set_env (script, "HOME", user_get_home_directory (user));
364     }
365     else
366         process_set_env (script, "HOME", "/");
367
368     SEAT_GET_CLASS (seat)->run_script (seat, display_server, script);
369
370     if (process_start (script, TRUE))
371     {
372         int exit_status;
373
374         exit_status = process_get_exit_status (script);
375         if (WIFEXITED (exit_status))
376         {
377             l_debug (seat, "Exit status of %s: %d", script_name, WEXITSTATUS (exit_status));
378             result = WEXITSTATUS (exit_status) == EXIT_SUCCESS;
379         }
380     }
381
382     g_object_unref (script);
383
384     return result;
385 }
386
387 static void
388 seat_real_run_script (Seat *seat, DisplayServer *display_server, Process *process)
389 {
390 }
391
392 static void
393 emit_upstart_signal (const gchar *signal)
394 {
395     g_return_if_fail (signal != NULL);
396     g_return_if_fail (signal[0] != 0);
397     const gchar* argv[] = {"initctl", "-q", "emit", signal, "DISPLAY_MANAGER=lightdm", NULL};
398
399     if (getuid () != 0)
400         return;
401
402     /* OK if it fails, probably not installed or not running upstart */
403     g_spawn_async (NULL, argv, NULL,
404             G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
405             NULL, NULL, NULL, NULL);
406 }
407
408 static void
409 check_stopped (Seat *seat)
410 {
411     if (seat->priv->stopping &&
412         !seat->priv->stopped &&
413         g_list_length (seat->priv->display_servers) == 0 &&
414         g_list_length (seat->priv->sessions) == 0)
415     {
416         seat->priv->stopped = TRUE;
417         l_debug (seat, "Stopped");
418         g_signal_emit (seat, signals[STOPPED], 0);
419     }
420 }
421
422 static void
423 display_server_stopped_cb (DisplayServer *display_server, Seat *seat)
424 {
425     const gchar *script;
426     GList *list, *link;
427     Session *active_session;
428
429     l_debug (seat, "Display server stopped");
430
431     /* Run a script right after stopping the display server */
432     script = seat_get_string_property (seat, "display-stopped-script");
433     if (script)
434         run_script (seat, NULL, script, NULL);
435
436     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
437     seat->priv->display_servers = g_list_remove (seat->priv->display_servers, display_server);
438
439     if (seat->priv->stopping || !seat->priv->started)
440     {
441         check_stopped (seat);
442         g_object_unref (display_server);
443         return;
444     }
445
446     /* Stop all sessions on this display server */
447     list = g_list_copy (seat->priv->sessions);
448     for (link = list; link; link = link->next)
449         g_object_ref (link->data);
450     for (link = list; link; link = link->next)
451     {
452         Session *session = link->data;
453
454         if (session_get_display_server (session) != display_server || session_get_is_stopping (session))
455             continue;
456
457         /* Stop seat if this is the only display server and it failed to start a greeter */
458         if (IS_GREETER (session) &&
459             !session_get_is_started (session) &&
460             g_list_length (seat->priv->display_servers) == 0)
461         {
462             l_debug (seat, "Stopping; greeter display server failed to start");
463             seat_stop (seat);
464         }
465
466         l_debug (seat, "Stopping session");
467         session_stop (session);
468     }
469     g_list_free_full (list, g_object_unref);
470
471     if (!seat->priv->stopping)
472     {
473         /* If we were the active session, switch to a greeter */
474         active_session = seat_get_active_session (seat);
475         if (!active_session || session_get_display_server (active_session) == display_server)
476         {
477             l_debug (seat, "Active display server stopped, starting greeter");
478             if (!seat_switch_to_greeter (seat))
479             {
480                 l_debug (seat, "Stopping; failed to start a greeter");
481                 seat_stop (seat);
482             }
483         }
484     }
485
486     g_object_unref (display_server);
487 }
488
489 static gboolean
490 can_share_display_server (Seat *seat, DisplayServer *display_server)
491 {
492     return seat->priv->share_display_server && display_server_get_can_share (display_server);
493 }
494
495 static Greeter *
496 find_greeter_session (Seat *seat)
497 {
498     GList *link;
499
500     for (link = seat->priv->sessions; link; link = link->next)
501     {
502         Session *session = link->data;
503         if (!session_get_is_stopping (session) && IS_GREETER (session))
504             return GREETER (session);
505     }
506
507     return NULL;
508 }
509
510 static Greeter *
511 find_resettable_greeter (Seat *seat)
512 {
513     GList *link;
514
515     for (link = seat->priv->sessions; link; link = link->next)
516     {
517         Session *session = link->data;
518         if (!session_get_is_stopping (session) && IS_GREETER (session) &&
519             greeter_get_resettable (GREETER (session)))
520             return GREETER (session);
521     }
522
523     return NULL;
524 }
525
526 static void
527 set_greeter_hints (Seat *seat, Greeter *greeter_session)
528 {
529     greeter_clear_hints (greeter_session);
530     greeter_set_hint (greeter_session, "default-session", seat_get_string_property (seat, "user-session"));
531     greeter_set_hint (greeter_session, "hide-users", seat_get_boolean_property (seat, "greeter-hide-users") ? "true" : "false");
532     greeter_set_hint (greeter_session, "show-manual-login", seat_get_boolean_property (seat, "greeter-show-manual-login") ? "true" : "false");
533     greeter_set_hint (greeter_session, "show-remote-login", seat_get_boolean_property (seat, "greeter-show-remote-login") ? "true" : "false");
534     greeter_set_hint (greeter_session, "has-guest-account", seat_get_allow_guest (seat) && seat_get_boolean_property (seat, "greeter-allow-guest") ? "true" : "false");
535 }
536
537 static void
538 switch_to_greeter_from_failed_session (Seat *seat, Session *session)
539 {
540     Greeter *greeter_session;
541     gboolean existing = FALSE;
542
543     /* Switch to greeter if one open */
544     greeter_session = find_resettable_greeter (seat);
545     if (greeter_session)
546     {
547         l_debug (seat, "Switching to existing greeter");
548         set_greeter_hints (seat, greeter_session);
549         existing = TRUE;
550     }
551     else
552     {
553         greeter_session = create_greeter_session (seat);
554     }
555
556     if (session_get_is_guest (session))
557         greeter_set_hint (greeter_session, "select-guest", "true");
558     else
559         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
560
561     if (existing)
562     {
563         greeter_reset (greeter_session);
564         seat_set_active_session (seat, SESSION (greeter_session));
565     }
566     else
567     {
568         if (seat->priv->session_to_activate)
569             g_object_unref (seat->priv->session_to_activate);
570         seat->priv->session_to_activate = g_object_ref (greeter_session);
571
572         if (can_share_display_server (seat, session_get_display_server (session)))
573             session_set_display_server (SESSION (greeter_session), session_get_display_server (session));
574         else
575         {
576             DisplayServer *display_server;
577
578             display_server = create_display_server (seat, session);
579             session_set_display_server (session, display_server);
580             if (!display_server_start (display_server))
581             {
582                 l_debug (seat, "Failed to start display server for greeter");
583                 seat_stop (seat);
584             }
585         }
586
587         start_session (seat, SESSION (greeter_session));
588     }
589
590     /* Stop failed session */
591     session_stop (session);
592 }
593
594 static void
595 start_session (Seat *seat, Session *session)
596 {
597     /* Use system location for greeter log file */
598     if (IS_GREETER (session))
599     {
600         gchar *log_dir, *filename, *log_filename;
601
602         log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
603         filename = g_strdup_printf ("%s-greeter.log", display_server_get_name (session_get_display_server (session)));
604         log_filename = g_build_filename (log_dir, filename, NULL);
605         g_free (log_dir);
606         g_free (filename);
607         session_set_log_file (session, log_filename);
608         g_free (log_filename);
609     }
610
611     if (session_start (session))
612         return;
613
614     if (IS_GREETER (session))
615     {
616         l_debug (seat, "Failed to start greeter");
617         display_server_stop (session_get_display_server (session));
618         return;
619     }
620
621     l_debug (seat, "Failed to start session, starting greeter");
622     switch_to_greeter_from_failed_session (seat, session);
623 }
624
625 static void
626 run_session (Seat *seat, Session *session)
627 {
628     const gchar *script;
629
630     if (IS_GREETER (session))
631         script = seat_get_string_property (seat, "greeter-setup-script");
632     else
633         script = seat_get_string_property (seat, "session-setup-script");
634     if (script && !run_script (seat, session_get_display_server (session), script, session_get_user (session)))
635     {
636         l_debug (seat, "Switching to greeter due to failed setup script");
637         switch_to_greeter_from_failed_session (seat, session);
638         return;
639     }
640
641     if (!IS_GREETER (session))
642     {
643         g_signal_emit (seat, signals[RUNNING_USER_SESSION], 0, session);
644         emit_upstart_signal ("desktop-session-start");
645     }
646
647     session_run (session);
648
649     // FIXME: Wait until the session is ready
650
651     if (session == seat->priv->session_to_activate)
652     {
653         seat_set_active_session (seat, session);
654         g_object_unref (seat->priv->session_to_activate);
655         seat->priv->session_to_activate = NULL;
656     }
657     else if (seat->priv->active_session)
658     {
659         /* Multiple sessions can theoretically be on the same VT (especially
660            if using Mir).  If a new session appears on an existing active VT,
661            logind will mark it as active, while ConsoleKit will re-mark the
662            oldest session as active.  In either case, that may not be the
663            session that we want to be active.  So let's be explicit and
664            re-activate the correct session whenever a new session starts.
665            There's no harm to do this in seats that enforce separate VTs. */
666         session_activate (seat->priv->active_session);
667     }
668 }
669
670 static Session *
671 find_user_session (Seat *seat, const gchar *username, Session *ignore_session)
672 {
673     GList *link;
674
675     if (!username)
676         return NULL;
677
678     for (link = seat->priv->sessions; link; link = link->next)
679     {
680         Session *session = link->data;
681
682         if (session == ignore_session)
683             continue;
684
685         if (!session_get_is_stopping (session) && g_strcmp0 (session_get_username (session), username) == 0)
686             return session;
687     }
688
689     return NULL;
690 }
691
692 static void
693 greeter_active_username_changed_cb (Greeter *greeter, GParamSpec *pspec, Seat *seat)
694 {
695     Session *session;
696
697     session = find_user_session (seat, greeter_get_active_username (greeter), seat->priv->active_session);
698
699     if (seat->priv->next_session)
700         g_object_unref (seat->priv->next_session);
701     seat->priv->next_session = session ? g_object_ref (session) : NULL;
702
703     SEAT_GET_CLASS (seat)->set_next_session (seat, session);
704 }
705
706 static void
707 session_authentication_complete_cb (Session *session, Seat *seat)
708 {
709     if (session_get_is_authenticated (session))
710     {
711         Session *s;
712
713         s = find_user_session (seat, session_get_username (session), session);
714         if (s)
715         {
716             l_debug (seat, "Session authenticated, switching to existing user session");
717             seat_set_active_session (seat, s);
718             session_stop (session);
719         }
720         else
721         {
722             l_debug (seat, "Session authenticated, running command");
723             run_session (seat, session);
724         }
725     }
726     else if (!IS_GREETER (session))
727     {
728         l_debug (seat, "Switching to greeter due to failed authentication");
729         switch_to_greeter_from_failed_session (seat, session);
730     }
731     else
732     {
733         l_debug (seat, "Stopping session that failed authentication");
734         session_stop (session);
735     }
736 }
737
738 static void
739 session_stopped_cb (Session *session, Seat *seat)
740 {
741     DisplayServer *display_server;
742
743     l_debug (seat, "Session stopped");
744
745     g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
746     seat->priv->sessions = g_list_remove (seat->priv->sessions, session);
747     if (session == seat->priv->active_session)
748     {
749         g_object_unref (seat->priv->active_session);
750         seat->priv->active_session = NULL;
751     }
752     if (session == seat->priv->next_session)
753     {
754         g_object_unref (seat->priv->next_session);
755         seat->priv->next_session = NULL;
756     }
757     if (session == seat->priv->session_to_activate)
758     {
759         g_object_unref (seat->priv->session_to_activate);
760         seat->priv->session_to_activate = NULL;
761     }
762
763     display_server = session_get_display_server (session);
764     if (!display_server)
765     {
766         g_object_unref (session);
767         return;
768     }
769
770     /* Cleanup */
771     if (!IS_GREETER (session))
772     {
773         const gchar *script;
774         script = seat_get_string_property (seat, "session-cleanup-script");
775         if (script)
776             run_script (seat, display_server, script, session_get_user (session));
777     }
778
779     /* We were waiting for this session, but it didn't start :( */
780     // FIXME: Start a greeter on this?
781     if (session == seat->priv->session_to_activate)
782     {
783         g_object_unref (seat->priv->session_to_activate);
784         seat->priv->session_to_activate = NULL;
785     }
786
787     if (seat->priv->stopping)
788     {
789         check_stopped (seat);
790         g_object_unref (session);
791         return;
792     }
793
794     /* If this is the greeter session then re-use this display server */
795     if (IS_GREETER (session) &&
796         can_share_display_server (seat, display_server) &&
797         greeter_get_start_session (GREETER (session)))
798     {
799         GList *link;
800
801         for (link = seat->priv->sessions; link; link = link->next)
802         {
803             Session *s = link->data;
804
805             /* Skip this session and sessions on other display servers */
806             if (s == session || session_get_display_server (s) != display_server || session_get_is_stopping (s))
807                 continue;
808
809             if (session_get_is_authenticated (s))
810             {
811                 l_debug (seat, "Greeter stopped, running session");
812                 run_session (seat, s);
813             }
814             else
815             {
816                 l_debug (seat, "Greeter stopped, starting session authentication");
817                 start_session (seat, s);
818             }
819             break;
820         }
821     }
822     /* If this is the greeter and nothing else is running then stop the seat */
823     else if (IS_GREETER (session) &&
824         !greeter_get_start_session (GREETER (session)) &&
825         g_list_length (seat->priv->display_servers) == 1 &&
826         g_list_nth_data (seat->priv->display_servers, 0) == display_server)
827     {
828         l_debug (seat, "Stopping; failed to start a greeter");
829         seat_stop (seat);
830     }
831     /* If we were the active session, switch to a greeter */
832     else if (!IS_GREETER (session) && session == seat_get_active_session (seat))
833     {
834         l_debug (seat, "Active session stopped, starting greeter");
835         if (!seat_switch_to_greeter (seat))
836         {
837             l_debug (seat, "Stopping; failed to start a greeter");
838             seat_stop (seat);
839         }
840     }
841
842     /* Stop the display server if no-longer required */
843     if (display_server && !display_server_get_is_stopping (display_server))
844     {
845         GList *link;
846         int n_sessions = 0;
847
848         for (link = seat->priv->sessions; link; link = link->next)
849         {
850             Session *s = link->data;
851             if (s == session)
852                 continue;
853             if (session_get_display_server (s) == display_server)
854                 n_sessions++;
855         }
856         if (n_sessions == 0)
857         {
858             l_debug (seat, "Stopping display server, no sessions require it");
859             display_server_stop (display_server);
860         }
861     }
862
863     g_signal_emit (seat, signals[SESSION_REMOVED], 0, session);
864     g_object_unref (session);
865 }
866
867 static void
868 set_session_env (Session *session)
869 {
870     /* Connect using the session bus */
871     if (getuid () != 0)
872     {
873         if (g_getenv ("DBUS_SESSION_BUS_ADDRESS"))
874             session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
875         session_set_env (session, "LDM_BUS", "SESSION");
876         if (g_getenv ("LD_PRELOAD"))
877             session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
878         if (g_getenv ("LD_LIBRARY_PATH"))
879             session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
880         if (g_getenv ("PATH"))
881             session_set_env (session, "PATH", g_getenv ("PATH"));
882     }
883
884     /* Variables required for regression tests */
885     if (g_getenv ("LIGHTDM_TEST_ROOT"))
886     {
887         session_set_env (session, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
888         session_set_env (session, "DBUS_SYSTEM_BUS_ADDRESS", g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
889         session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
890         session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
891         session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
892         session_set_env (session, "GI_TYPELIB_PATH", g_getenv ("GI_TYPELIB_PATH"));
893     }
894 }
895
896 static Session *
897 create_session (Seat *seat, gboolean autostart)
898 {
899     Session *session;
900
901     session = SEAT_GET_CLASS (seat)->create_session (seat);
902     seat->priv->sessions = g_list_append (seat->priv->sessions, session);
903     if (autostart)
904         g_signal_connect (session, SESSION_SIGNAL_AUTHENTICATION_COMPLETE, G_CALLBACK (session_authentication_complete_cb), seat);
905     g_signal_connect (session, SESSION_SIGNAL_STOPPED, G_CALLBACK (session_stopped_cb), seat);
906
907     set_session_env (session);
908
909     g_signal_emit (seat, signals[SESSION_ADDED], 0, session);
910
911     return session;
912 }
913
914 static gchar **
915 get_session_argv (Seat *seat, SessionConfig *session_config, const gchar *session_wrapper)
916 {
917     gboolean result;
918     int argc;
919     gchar **argv, *path;
920     GError *error = NULL;
921
922     /* If configured, run sessions through a wrapper */
923     if (session_wrapper)
924     {
925         argv = g_malloc (sizeof (gchar *) * 3);
926         path = g_find_program_in_path (session_wrapper);
927         argv[0] = path ? path : g_strdup (session_wrapper);
928         argv[1] = g_strdup (session_config_get_command (session_config));
929         argv[2] = NULL;
930         return argv;
931     }
932
933     /* Split command into an array listing and make command absolute */
934     result = g_shell_parse_argv (session_config_get_command (session_config), &argc, &argv, &error);
935     if (error)
936         l_debug (seat, "Invalid session command '%s': %s", session_config_get_command (session_config), error->message);
937     g_clear_error (&error);
938     if (!result)
939         return NULL;
940     path = g_find_program_in_path (argv[0]);
941     if (path)
942     {
943         g_free (argv[0]);
944         argv[0] = path;
945     }
946
947     return argv;
948 }
949
950 static SessionConfig *
951 find_session_config (Seat *seat, const gchar *sessions_dir, const gchar *session_name)
952 {
953     gchar **dirs;
954     SessionConfig *session_config = NULL;
955     int i;
956     GError *error = NULL;
957
958     g_return_val_if_fail (sessions_dir != NULL, NULL);
959     g_return_val_if_fail (session_name != NULL, NULL);
960
961     dirs = g_strsplit (sessions_dir, ":", -1);
962     for (i = 0; dirs[i]; i++)
963     {
964         gchar *filename, *path;
965         const gchar *default_session_type = "x";
966
967         if (strcmp (dirs[i], WAYLAND_SESSIONS_DIR) == 0)
968             default_session_type = "wayland";
969
970         filename = g_strdup_printf ("%s.desktop", session_name);
971         path = g_build_filename (dirs[i], filename, NULL);
972         g_free (filename);
973         session_config = session_config_new_from_file (path, default_session_type, &error);
974         g_free (path);
975         if (session_config)
976             break;
977
978         if (dirs[i+1] == NULL)
979             l_debug (seat, "Failed to find session configuration %s", session_name);
980         g_clear_error (&error);
981     }
982     g_strfreev (dirs);
983
984     return session_config;
985 }
986
987 static void
988 configure_session (Session *session, SessionConfig *config, const gchar *session_name, const gchar *language)
989 {
990     gchar **desktop_names;
991
992     session_set_config (session, config);
993     session_set_env (session, "XDG_SESSION_DESKTOP", session_name);
994     session_set_env (session, "DESKTOP_SESSION", session_name);
995     session_set_env (session, "GDMSESSION", session_name);
996     desktop_names = session_config_get_desktop_names (config);
997     if (desktop_names)
998     {
999         gchar *value;
1000         value = g_strjoinv (":", desktop_names);
1001         session_set_env (session, "XDG_CURRENT_DESKTOP", value);
1002         g_free (value);
1003     }
1004     if (language && language[0] != '\0')
1005     {
1006         session_set_env (session, "LANG", language);
1007         session_set_env (session, "GDM_LANG", language);
1008     }
1009 }
1010
1011 static Session *
1012 create_user_session (Seat *seat, const gchar *username, gboolean autostart)
1013 {
1014     User *user;
1015     gchar *sessions_dir;
1016     const gchar *session_name, *language;
1017     SessionConfig *session_config;
1018     Session *session = NULL;
1019
1020     l_debug (seat, "Creating user session");
1021
1022     /* Load user preferences */
1023     user = accounts_get_user_by_name (username);
1024     if (!user)
1025     {
1026         l_debug (seat, "Can't login unknown user '%s'", username);
1027         return NULL;
1028     }
1029     session_name = user_get_xsession (user);
1030     language = user_get_language (user);
1031
1032     if (!session_name)
1033         session_name = seat_get_string_property (seat, "user-session");
1034     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
1035     session_config = find_session_config (seat, sessions_dir, session_name);
1036     g_free (sessions_dir);
1037     if (session_config)
1038     {
1039         gchar **argv;
1040
1041         session = create_session (seat, autostart);
1042         configure_session (session, session_config, session_name, language);
1043         session_set_username (session, username);
1044         session_set_do_authenticate (session, TRUE);
1045         argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
1046         session_set_argv (session, argv);
1047         g_strfreev (argv);
1048         g_object_unref (session_config);
1049     }
1050     else
1051         l_debug (seat, "Can't find session '%s'", session_name);
1052
1053     g_object_unref (user);
1054
1055     return session;
1056 }
1057
1058 static void
1059 prepend_argv (gchar ***argv, const gchar *value)
1060 {
1061     gchar **old_argv, **new_argv;
1062     gint i;
1063
1064     old_argv = *argv;
1065     new_argv = g_malloc (sizeof (gchar *) * (g_strv_length (*argv) + 2));
1066     new_argv[0] = g_strdup (value);
1067     for (i = 0; old_argv[i]; i++)
1068         new_argv[i + 1] = old_argv[i];
1069     new_argv[i + 1] = NULL;
1070
1071     g_free (*argv);
1072     *argv = new_argv;
1073 }
1074
1075 static Session *
1076 create_guest_session (Seat *seat, const gchar *session_name)
1077 {
1078     const gchar *guest_wrapper;
1079     gchar *sessions_dir, **argv;
1080     SessionConfig *session_config;
1081     Session *session;
1082
1083     if (!session_name)
1084         session_name = seat_get_string_property (seat, "guest-session");
1085     if (!session_name)
1086         session_name = seat_get_string_property (seat, "user-session");
1087     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
1088     session_config = find_session_config (seat, sessions_dir, session_name);
1089     g_free (sessions_dir);
1090     if (!session_config)
1091     {
1092         l_debug (seat, "Can't find session '%s'", session_name);
1093         return NULL;
1094     }
1095
1096     session = create_session (seat, TRUE);
1097     configure_session (session, session_config, session_name, NULL);
1098     session_set_do_authenticate (session, TRUE);
1099     session_set_is_guest (session, TRUE);
1100     argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
1101     guest_wrapper = seat_get_string_property (seat, "guest-wrapper");
1102     if (guest_wrapper)
1103     {
1104         gchar *path;
1105         path = g_find_program_in_path (guest_wrapper);
1106         prepend_argv (&argv, path ? path : guest_wrapper);
1107         g_free (path);
1108     }
1109
1110     session_set_argv (session, argv);
1111     g_strfreev (argv);
1112     g_object_unref (session_config);
1113
1114     return session;
1115 }
1116
1117 static Session *
1118 greeter_create_session_cb (Greeter *greeter, Seat *seat)
1119 {
1120     Session *session;
1121
1122     session = create_session (seat, FALSE);
1123     session_set_config (session, session_get_config (SESSION (greeter)));
1124     session_set_display_server (session, session_get_display_server (SESSION (greeter)));
1125
1126     return g_object_ref (session);
1127 }
1128
1129 static gboolean
1130 greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *session_name, Seat *seat)
1131 {
1132     Session *session, *existing_session;
1133     const gchar *username;
1134     DisplayServer *display_server;
1135
1136     /* Get the session to use */
1137     if (greeter_get_guest_authenticated (greeter))
1138     {
1139         session = create_guest_session (seat, session_name);
1140         if (!session)
1141             return FALSE;
1142         session_set_pam_service (session, seat_get_string_property (seat, "pam-autologin-service"));
1143     }
1144     else
1145     {
1146         const gchar *language = NULL;
1147         SessionConfig *session_config;
1148         User *user;
1149         gchar *sessions_dir = NULL;
1150         gchar **argv;
1151
1152         session = greeter_get_authentication_session (greeter);
1153
1154         /* Get session command to run */
1155         switch (type)
1156         {
1157         case SESSION_TYPE_LOCAL:
1158             sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
1159             break;
1160         case SESSION_TYPE_REMOTE:
1161             sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
1162             break;
1163         }
1164
1165         /* Load user preferences */
1166         user = session_get_user (session);
1167         if (user)
1168         {
1169             if (!session_name)
1170                 session_name = user_get_xsession (user);
1171             language = user_get_language (user);
1172         }
1173
1174         if (!session_name)
1175             session_name = seat_get_string_property (seat, "user-session");
1176         if (user)
1177             user_set_xsession (session_get_user (session), session_name);
1178
1179         session_config = find_session_config (seat, sessions_dir, session_name);
1180         g_free (sessions_dir);
1181         if (!session_config)
1182         {
1183             l_debug (seat, "Can't find session '%s'", session_name);
1184             return FALSE;
1185         }
1186
1187         configure_session (session, session_config, session_name, language);
1188         argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
1189         session_set_argv (session, argv);
1190         g_strfreev (argv);
1191         g_object_unref (session_config);
1192     }
1193
1194     /* Switch to this session when it is ready */
1195     if (seat->priv->session_to_activate)
1196         g_object_unref (seat->priv->session_to_activate);
1197     seat->priv->session_to_activate = g_object_ref (session);
1198
1199     /* Return to existing session if it is open */
1200     username = session_get_username (session);
1201     existing_session = find_user_session (seat, username, NULL);
1202     if (existing_session && session != existing_session)
1203     {
1204         l_debug (seat, "Returning to existing user session %s", username);
1205         session_stop (session);
1206         session_unlock (existing_session);
1207         seat_set_active_session (seat, existing_session);
1208         return TRUE;
1209     }
1210
1211     /* If can re-use the display server, stop the greeter first */
1212     display_server = session_get_display_server (SESSION (greeter));
1213     if (!greeter_get_resettable (greeter) &&
1214         can_share_display_server (seat, display_server) &&
1215         strcmp (display_server_get_session_type (display_server), session_get_session_type (session)) == 0)
1216     {
1217         l_debug (seat, "Stopping greeter; display server will be re-used for user session");
1218
1219         /* Run on the same display server after the greeter has stopped */
1220         session_set_display_server (session, display_server);
1221
1222         /* Stop the greeter */
1223         session_stop (SESSION (greeter));
1224
1225         return TRUE;
1226     }
1227     /* Otherwise start a new display server for this session */
1228     else
1229     {
1230         display_server = create_display_server (seat, session);
1231         session_set_display_server (session, display_server);
1232         if (!display_server_start (display_server))
1233         {
1234             l_debug (seat, "Failed to start display server for new session");
1235             return FALSE;
1236         }
1237
1238         return TRUE;
1239     }
1240 }
1241
1242 static Greeter *
1243 create_greeter_session (Seat *seat)
1244 {
1245     gchar *sessions_dir, **argv;
1246     SessionConfig *session_config;
1247     Greeter *greeter_session;
1248     const gchar *greeter_wrapper;
1249     const gchar *autologin_username;
1250     int autologin_timeout;
1251     gboolean autologin_guest;
1252
1253     l_debug (seat, "Creating greeter session");
1254
1255     sessions_dir = config_get_string (config_get_instance (), "LightDM", "greeters-directory");
1256     session_config = find_session_config (seat, sessions_dir, seat_get_string_property (seat, "greeter-session"));
1257     g_free (sessions_dir);
1258     if (!session_config)
1259         return NULL;
1260
1261     argv = get_session_argv (seat, session_config, NULL);
1262     greeter_wrapper = seat_get_string_property (seat, "greeter-wrapper");
1263     if (greeter_wrapper)
1264     {
1265         gchar *path;
1266         path = g_find_program_in_path (greeter_wrapper);
1267         prepend_argv (&argv, path ? path : greeter_wrapper);
1268         g_free (path);
1269     }
1270
1271     greeter_session = SEAT_GET_CLASS (seat)->create_greeter_session (seat);
1272     session_set_config (SESSION (greeter_session), session_config);
1273     seat->priv->sessions = g_list_append (seat->priv->sessions, SESSION (greeter_session));
1274     g_signal_connect (greeter_session, GREETER_SIGNAL_ACTIVE_USERNAME_CHANGED, G_CALLBACK (greeter_active_username_changed_cb), seat);
1275     g_signal_connect (greeter_session, SESSION_SIGNAL_AUTHENTICATION_COMPLETE, G_CALLBACK (session_authentication_complete_cb), seat);
1276     g_signal_connect (greeter_session, SESSION_SIGNAL_STOPPED, G_CALLBACK (session_stopped_cb), seat);
1277
1278     set_session_env (SESSION (greeter_session));
1279     session_set_env (SESSION (greeter_session), "XDG_SESSION_CLASS", "greeter");
1280
1281     session_set_pam_service (SESSION (greeter_session), seat_get_string_property (seat, "pam-greeter-service"));
1282     if (getuid () == 0)
1283     {
1284         gchar *greeter_user;
1285         greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
1286         session_set_username (SESSION (greeter_session), greeter_user);
1287         g_free (greeter_user);
1288     }
1289     else
1290     {
1291         /* In test mode run the greeter as ourself */
1292         session_set_username (SESSION (greeter_session), user_get_name (accounts_get_current_user ()));
1293     }
1294     session_set_argv (SESSION (greeter_session), argv);
1295     g_strfreev (argv);
1296
1297     greeter_set_pam_services (greeter_session,
1298                               seat_get_string_property (seat, "pam-service"),
1299                               seat_get_string_property (seat, "pam-autologin-service"));
1300     g_signal_connect (greeter_session, GREETER_SIGNAL_CREATE_SESSION, G_CALLBACK (greeter_create_session_cb), seat);
1301     g_signal_connect (greeter_session, GREETER_SIGNAL_START_SESSION, G_CALLBACK (greeter_start_session_cb), seat);
1302
1303     /* Set hints to greeter */
1304     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
1305     set_greeter_hints (seat, greeter_session);
1306
1307     /* Configure for automatic login */
1308     autologin_username = seat_get_string_property (seat, "autologin-user");
1309     if (g_strcmp0 (autologin_username, "") == 0)
1310         autologin_username = NULL;
1311     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1312     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1313     if (autologin_timeout > 0)
1314     {
1315         gchar *value;
1316
1317         value = g_strdup_printf ("%d", autologin_timeout);
1318         greeter_set_hint (greeter_session, "autologin-timeout", value);
1319         g_free (value);
1320         if (autologin_username)
1321             greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1322         if (autologin_guest)
1323             greeter_set_hint (greeter_session, "autologin-guest", "true");
1324     }
1325
1326     g_object_unref (session_config);
1327
1328     return greeter_session;
1329 }
1330
1331 static Session *
1332 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
1333 {
1334     GList *link;
1335
1336     for (link = seat->priv->sessions; link; link = link->next)
1337     {
1338         Session *session = link->data;
1339         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
1340             return session;
1341     }
1342
1343     return NULL;
1344 }
1345
1346 static void
1347 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
1348 {
1349     const gchar *script;
1350     Session *session;
1351
1352     /* Run setup script */
1353     script = seat_get_string_property (seat, "display-setup-script");
1354     if (script && !run_script (seat, display_server, script, NULL))
1355     {
1356         l_debug (seat, "Stopping display server due to failed setup script");
1357         display_server_stop (display_server);
1358         return;
1359     }
1360
1361     emit_upstart_signal ("login-session-start");
1362
1363     /* Start the session waiting for this display server */
1364     session = find_session_for_display_server (seat, display_server);
1365     if (session)
1366     {
1367         if (session_get_is_authenticated (session))
1368         {
1369             l_debug (seat, "Display server ready, running session");
1370             run_session (seat, session);
1371         }
1372         else
1373         {
1374             l_debug (seat, "Display server ready, starting session authentication");
1375             start_session (seat, session);
1376         }
1377     }
1378     else
1379     {
1380         l_debug (seat, "Stopping not required display server");
1381         display_server_stop (display_server);
1382     }
1383 }
1384
1385 static DisplayServer *
1386 create_display_server (Seat *seat, Session *session)
1387 {
1388     DisplayServer *display_server;
1389
1390     l_debug (seat, "Creating display server of type %s", session_get_session_type (session));
1391
1392     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat, session);
1393     if (!display_server)
1394         return NULL;
1395
1396     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1397     g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_READY, G_CALLBACK (display_server_ready_cb), seat);
1398     g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (display_server_stopped_cb), seat);
1399
1400     return display_server;
1401 }
1402
1403 gboolean
1404 seat_switch_to_greeter (Seat *seat)
1405 {
1406     Greeter *greeter_session;
1407     DisplayServer *display_server;
1408
1409     g_return_val_if_fail (seat != NULL, FALSE);
1410
1411     if (!seat_get_can_switch (seat))
1412         return FALSE;
1413
1414     /* Switch to greeter if one open */
1415     greeter_session = find_greeter_session (seat);
1416     if (greeter_session)
1417     {
1418         l_debug (seat, "Switching to existing greeter");
1419         seat_set_active_session (seat, SESSION (greeter_session));
1420         return TRUE;
1421     }
1422
1423     greeter_session = create_greeter_session (seat);
1424     if (!greeter_session)
1425         return FALSE;
1426
1427     if (seat->priv->session_to_activate)
1428         g_object_unref (seat->priv->session_to_activate);
1429     seat->priv->session_to_activate = g_object_ref (greeter_session);
1430
1431     display_server = create_display_server (seat, SESSION (greeter_session));
1432     session_set_display_server (SESSION (greeter_session), display_server);
1433
1434     return display_server_start (display_server);
1435 }
1436
1437 static void
1438 switch_authentication_complete_cb (Session *session, Seat *seat)
1439 {
1440     Greeter *greeter_session;
1441     DisplayServer *display_server;
1442     gboolean existing = FALSE;
1443
1444     /* If authenticated, then unlock existing session or start new one */
1445     if (session_get_is_authenticated (session))
1446     {
1447         Session *s;
1448
1449         s = find_user_session (seat, session_get_username (session), session);
1450         if (s)
1451         {
1452             l_debug (seat, "Session authenticated, switching to existing user session");
1453             session_unlock (s);
1454             seat_set_active_session (seat, s);
1455             session_stop (session);
1456         }
1457         else
1458         {
1459             l_debug (seat, "Session authenticated, starting display server");
1460             if (seat->priv->session_to_activate)
1461                 g_object_unref (seat->priv->session_to_activate);
1462             seat->priv->session_to_activate = g_object_ref (session);
1463             display_server = create_display_server (seat, session);
1464             session_set_display_server (session, display_server);
1465             display_server_start (display_server);
1466         }
1467
1468         return;
1469     }
1470
1471     session_stop (session);
1472
1473     /* See if we already have a greeter up and reuse it if so */
1474     greeter_session = find_resettable_greeter (seat);
1475     if (greeter_session)
1476     {
1477         l_debug (seat, "Switching to existing greeter to authenticate session");
1478         set_greeter_hints (seat, greeter_session);
1479         existing = TRUE;
1480     }
1481     else
1482     {
1483         l_debug (seat, "Starting greeter to authenticate session");
1484         greeter_session = create_greeter_session (seat);
1485     }
1486
1487     if (session_get_is_guest (session))
1488         greeter_set_hint (greeter_session, "select-guest", "true");
1489     else
1490         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
1491
1492     if (existing)
1493     {
1494         greeter_reset (greeter_session);
1495         seat_set_active_session (seat, SESSION (greeter_session));
1496     }
1497     else
1498     {
1499         if (seat->priv->session_to_activate)
1500             g_object_unref (seat->priv->session_to_activate);
1501         seat->priv->session_to_activate = g_object_ref (greeter_session);
1502
1503         display_server = create_display_server (seat, SESSION (greeter_session));
1504         session_set_display_server (SESSION (greeter_session), display_server);
1505         display_server_start (display_server);
1506     }
1507 }
1508
1509 gboolean
1510 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1511 {
1512     Session *session;
1513
1514     g_return_val_if_fail (seat != NULL, FALSE);
1515     g_return_val_if_fail (username != NULL, FALSE);
1516
1517     if (!seat_get_can_switch (seat))
1518         return FALSE;
1519
1520     /* If we're already on this session, then ignore */
1521     session = find_user_session (seat, username, NULL);
1522     if (session && session == seat->priv->active_session)
1523         return TRUE;
1524
1525     l_debug (seat, "Switching to user %s", username);
1526
1527     /* Attempt to authenticate them */
1528     session = create_user_session (seat, username, FALSE);
1529     g_signal_connect (session, SESSION_SIGNAL_AUTHENTICATION_COMPLETE, G_CALLBACK (switch_authentication_complete_cb), seat);
1530     session_set_pam_service (session, seat_get_string_property (seat, "pam-service"));
1531
1532     return session_start (session);
1533 }
1534
1535 static Session *
1536 find_guest_session (Seat *seat)
1537 {
1538     GList *link;
1539
1540     for (link = seat->priv->sessions; link; link = link->next)
1541     {
1542         Session *session = link->data;
1543         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1544             return session;
1545     }
1546
1547     return NULL;
1548 }
1549
1550 gboolean
1551 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1552 {
1553     Session *session;
1554     DisplayServer *display_server;
1555
1556     g_return_val_if_fail (seat != NULL, FALSE);
1557
1558     if (!seat_get_can_switch (seat) || !seat_get_allow_guest (seat))
1559         return FALSE;
1560
1561     /* Switch to session if one open */
1562     session = find_guest_session (seat);
1563     if (session)
1564     {
1565         l_debug (seat, "Switching to existing guest account %s", session_get_username (session));
1566         seat_set_active_session (seat, session);
1567         return TRUE;
1568     }
1569
1570     session = create_guest_session (seat, session_name);
1571     if (!session)
1572         return FALSE;
1573
1574     display_server = create_display_server (seat, session);
1575
1576     if (seat->priv->session_to_activate)
1577         g_object_unref (seat->priv->session_to_activate);
1578     seat->priv->session_to_activate = g_object_ref (session);
1579     session_set_pam_service (session, seat_get_string_property (seat, "pam-autologin-service"));
1580     session_set_display_server (session, display_server);
1581
1582     return display_server_start (display_server);
1583 }
1584
1585 gboolean
1586 seat_lock (Seat *seat, const gchar *username)
1587 {
1588     Greeter *greeter_session;
1589     DisplayServer *display_server;
1590     gboolean existing = FALSE;
1591
1592     g_return_val_if_fail (seat != NULL, FALSE);
1593
1594     if (!seat_get_can_switch (seat))
1595         return FALSE;
1596
1597     l_debug (seat, "Locking");
1598
1599     /* Switch to greeter we can reuse */
1600     greeter_session = find_resettable_greeter (seat);
1601     if (greeter_session)
1602     {
1603         l_debug (seat, "Switching to existing greeter");
1604         set_greeter_hints (seat, greeter_session);
1605         existing = TRUE;
1606     }
1607     else
1608     {
1609         greeter_session = create_greeter_session (seat);
1610         if (!greeter_session)
1611             return FALSE;
1612     }
1613
1614     greeter_set_hint (greeter_session, "lock-screen", "true");
1615     if (username)
1616         greeter_set_hint (greeter_session, "select-user", username);
1617
1618     if (existing)
1619     {
1620         greeter_reset (greeter_session);
1621         seat_set_active_session (seat, SESSION (greeter_session));
1622         return TRUE;
1623     }
1624     else
1625     {
1626         display_server = create_display_server (seat, SESSION (greeter_session));
1627
1628         if (seat->priv->session_to_activate)
1629             g_object_unref (seat->priv->session_to_activate);
1630         seat->priv->session_to_activate = g_object_ref (greeter_session);
1631         session_set_display_server (SESSION (greeter_session), display_server);
1632
1633         return display_server_start (display_server);
1634     }
1635 }
1636
1637 void
1638 seat_stop (Seat *seat)
1639 {
1640     g_return_if_fail (seat != NULL);
1641
1642     if (seat->priv->stopping)
1643         return;
1644
1645     l_debug (seat, "Stopping");
1646     seat->priv->stopping = TRUE;
1647     SEAT_GET_CLASS (seat)->stop (seat);
1648 }
1649
1650 gboolean
1651 seat_get_is_stopping (Seat *seat)
1652 {
1653     g_return_val_if_fail (seat != NULL, FALSE);
1654     return seat->priv->stopping;
1655 }
1656
1657 static void
1658 seat_real_setup (Seat *seat)
1659 {
1660 }
1661
1662 static gboolean
1663 seat_real_start (Seat *seat)
1664 {
1665     const gchar *autologin_username;
1666     int autologin_timeout;
1667     gboolean autologin_guest;
1668     gboolean autologin_in_background;
1669     Session *session = NULL, *background_session = NULL;
1670
1671     /* Get autologin settings */
1672     autologin_username = seat_get_string_property (seat, "autologin-user");
1673     if (g_strcmp0 (autologin_username, "") == 0)
1674         autologin_username = NULL;
1675     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1676     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1677     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1678
1679     /* Autologin if configured */
1680     if (autologin_timeout == 0 || autologin_in_background)
1681     {
1682         if (autologin_guest)
1683             session = create_guest_session (seat, NULL);
1684         else if (autologin_username != NULL)
1685             session = create_user_session (seat, autologin_username, TRUE);
1686
1687         if (session)
1688             session_set_pam_service (session, seat_get_string_property (seat, "pam-autologin-service"));
1689
1690         /* Load in background if required */
1691         if (autologin_in_background && session)
1692         {
1693             background_session = session;
1694             session = NULL;
1695         }
1696
1697         if (session)
1698         {
1699             DisplayServer *display_server;
1700
1701             if (seat->priv->session_to_activate)
1702                 g_object_unref (seat->priv->session_to_activate);
1703             seat->priv->session_to_activate = g_object_ref (session);
1704
1705             display_server = create_display_server (seat, session);
1706             session_set_display_server (session, display_server);
1707             if (!display_server || !display_server_start (display_server))
1708             {
1709                 l_debug (seat, "Can't create display server for automatic login");
1710                 session_stop (session);
1711                 if (display_server)
1712                     display_server_stop (display_server);
1713                 session = NULL;
1714             }
1715         }
1716     }
1717
1718     /* Fallback to a greeter */
1719     if (!session)
1720     {
1721         Greeter *greeter_session;
1722         DisplayServer *display_server;
1723
1724         greeter_session = create_greeter_session (seat);
1725         if (!greeter_session)
1726         {
1727             l_debug (seat, "Failed to create greeter session");
1728             return FALSE;
1729         }
1730
1731         if (seat->priv->session_to_activate)
1732             g_object_unref (seat->priv->session_to_activate);
1733         seat->priv->session_to_activate = g_object_ref (greeter_session);
1734         session = SESSION (greeter_session);
1735
1736         display_server = create_display_server (seat, session);
1737         session_set_display_server (session, display_server);
1738         if (!display_server || !display_server_start (display_server))
1739         {
1740             l_debug (seat, "Can't create display server for greeter");
1741             session_stop (session);
1742             if (display_server)
1743                 display_server_stop (display_server);
1744             session = NULL;
1745         }
1746     }
1747
1748     /* Fail if can't start a session */
1749     if (!session)
1750     {
1751         seat_stop (seat);
1752         return FALSE;
1753     }
1754
1755     /* Start background session */
1756     if (background_session)
1757     {
1758         DisplayServer *background_display_server;
1759
1760         background_display_server = create_display_server (seat, background_session);
1761         session_set_display_server (background_session, background_display_server);
1762         if (!display_server_start (background_display_server))
1763             l_warning (seat, "Failed to start display server for background session");
1764     }
1765
1766     return TRUE;
1767 }
1768
1769 static Greeter *
1770 seat_real_create_greeter_session (Seat *seat)
1771 {
1772     return greeter_new ();
1773 }
1774
1775 static Session *
1776 seat_real_create_session (Seat *seat)
1777 {
1778     return session_new ();
1779 }
1780
1781 static void
1782 seat_real_set_active_session (Seat *seat, Session *session)
1783 {
1784 }
1785
1786 static void
1787 seat_real_set_next_session (Seat *seat, Session *session)
1788 {
1789 }
1790
1791 static Session *
1792 seat_real_get_active_session (Seat *seat)
1793 {
1794     return NULL;
1795 }
1796
1797 static void
1798 seat_real_stop (Seat *seat)
1799 {
1800     GList *list, *link;
1801
1802     check_stopped (seat);
1803     if (seat->priv->stopped)
1804         return;
1805
1806     /* Stop all the display servers and sessions on the seat. Copy the list as
1807      * it might be modified if a display server / session stops during this loop */
1808     list = g_list_copy (seat->priv->display_servers);
1809     for (link = list; link; link = link->next)
1810         g_object_ref (link->data);
1811     for (link = list; link; link = link->next)
1812     {
1813         DisplayServer *display_server = link->data;
1814         if (!display_server_get_is_stopping (display_server))
1815         {
1816             l_debug (seat, "Stopping display server");
1817             display_server_stop (display_server);
1818         }
1819     }
1820     g_list_free_full (list, g_object_unref);
1821     list = g_list_copy (seat->priv->sessions);
1822     for (link = list; link; link = link->next)
1823         g_object_ref (link->data);
1824     for (link = list; link; link = link->next)
1825     {
1826         Session *session = link->data;
1827         if (!session_get_is_stopping (session))
1828         {
1829             l_debug (seat, "Stopping session");
1830             session_stop (session);
1831         }
1832     }
1833     g_list_free_full (list, g_object_unref);
1834 }
1835
1836 static void
1837 seat_init (Seat *seat)
1838 {
1839     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1840     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1841     seat->priv->share_display_server = TRUE;
1842 }
1843
1844 static void
1845 seat_finalize (GObject *object)
1846 {
1847     Seat *self;
1848     GList *link;
1849
1850     self = SEAT (object);
1851
1852     g_free (self->priv->name);
1853     g_hash_table_unref (self->priv->properties);
1854     for (link = self->priv->display_servers; link; link = link->next)
1855     {
1856         DisplayServer *display_server = link->data;
1857         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1858     }
1859     g_list_free_full (self->priv->display_servers, g_object_unref);
1860     for (link = self->priv->sessions; link; link = link->next)
1861     {
1862         Session *session = link->data;
1863         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1864     }
1865     g_list_free_full (self->priv->sessions, g_object_unref);
1866     if (self->priv->active_session)
1867         g_object_unref (self->priv->active_session);
1868     if (self->priv->next_session)
1869         g_object_unref (self->priv->next_session);
1870     if (self->priv->session_to_activate)
1871         g_object_unref (self->priv->session_to_activate);
1872
1873     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1874 }
1875
1876 static void
1877 seat_class_init (SeatClass *klass)
1878 {
1879     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1880
1881     klass->setup = seat_real_setup;
1882     klass->start = seat_real_start;
1883     klass->create_greeter_session = seat_real_create_greeter_session;
1884     klass->create_session = seat_real_create_session;
1885     klass->set_active_session = seat_real_set_active_session;
1886     klass->get_active_session = seat_real_get_active_session;
1887     klass->set_next_session = seat_real_set_next_session;
1888     klass->run_script = seat_real_run_script;
1889     klass->stop = seat_real_stop;
1890
1891     object_class->finalize = seat_finalize;
1892
1893     g_type_class_add_private (klass, sizeof (SeatPrivate));
1894
1895     signals[SESSION_ADDED] =
1896         g_signal_new (SEAT_SIGNAL_SESSION_ADDED,
1897                       G_TYPE_FROM_CLASS (klass),
1898                       G_SIGNAL_RUN_LAST,
1899                       G_STRUCT_OFFSET (SeatClass, session_added),
1900                       NULL, NULL,
1901                       NULL,
1902                       G_TYPE_NONE, 1, SESSION_TYPE);
1903     signals[RUNNING_USER_SESSION] =
1904         g_signal_new (SEAT_SIGNAL_RUNNING_USER_SESSION,
1905                       G_TYPE_FROM_CLASS (klass),
1906                       G_SIGNAL_RUN_LAST,
1907                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1908                       NULL, NULL,
1909                       NULL,
1910                       G_TYPE_NONE, 1, SESSION_TYPE);
1911     signals[SESSION_REMOVED] =
1912         g_signal_new (SEAT_SIGNAL_SESSION_REMOVED,
1913                       G_TYPE_FROM_CLASS (klass),
1914                       G_SIGNAL_RUN_LAST,
1915                       G_STRUCT_OFFSET (SeatClass, session_removed),
1916                       NULL, NULL,
1917                       NULL,
1918                       G_TYPE_NONE, 1, SESSION_TYPE);
1919     signals[STOPPED] =
1920         g_signal_new (SEAT_SIGNAL_STOPPED,
1921                       G_TYPE_FROM_CLASS (klass),
1922                       G_SIGNAL_RUN_LAST,
1923                       G_STRUCT_OFFSET (SeatClass, stopped),
1924                       NULL, NULL,
1925                       NULL,
1926                       G_TYPE_NONE, 0);
1927 }
1928
1929 static gint
1930 seat_real_logprefix (Logger *self, gchar *buf, gulong buflen)
1931 {
1932     return g_snprintf (buf, buflen, "Seat %s: ", SEAT (self)->priv->name);
1933 }
1934
1935 static void
1936 seat_logger_iface_init (LoggerInterface *iface)
1937 {
1938     iface->logprefix = &seat_real_logprefix;
1939 }