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