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