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