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