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