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