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