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