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