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