]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Honour session type requested by greeter for guest sessions
[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 (NULL, NULL);
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, const gchar *session_name)
928 {
929     const gchar *guest_wrapper;
930     gchar *sessions_dir, **argv;
931     SessionConfig *session_config;
932     Session *session;
933
934     if (!session_name)
935         session_name = seat_get_string_property (seat, "user-session");
936     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
937     session_config = find_session_config (seat, sessions_dir, session_name);
938     g_free (sessions_dir);
939     if (!session_config)
940     {
941         l_debug (seat, "Can't find session '%s'", seat_get_string_property (seat, "user-session"));
942         return NULL;
943     }
944
945     session = create_session (seat, TRUE);
946     configure_session (session, session_config, session_name, NULL);
947     session_set_do_authenticate (session, TRUE);
948     session_set_is_guest (session, TRUE);
949     argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
950     guest_wrapper = seat_get_string_property (seat, "guest-wrapper");
951     if (guest_wrapper)
952     {
953         gchar *path;
954         path = g_find_program_in_path (guest_wrapper);
955         prepend_argv (&argv, path ? path : guest_wrapper);
956         g_free (path);
957     }
958
959     session_set_argv (session, argv);
960     g_strfreev (argv);
961     g_object_unref (session_config);
962   
963     return session;
964 }
965
966 static Session *
967 greeter_create_session_cb (Greeter *greeter, Seat *seat)
968 {
969     Session *session;
970
971     session = create_session (seat, FALSE);
972     session_set_session_type (session, session_get_session_type (SESSION (greeter)));
973     session_set_display_server (session, session_get_display_server (SESSION (greeter)));
974
975     return g_object_ref (session);
976 }
977
978 static gboolean
979 greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *session_name, Seat *seat)
980 {
981     Session *session, *existing_session;
982     const gchar *username;
983     DisplayServer *display_server;
984
985     /* Get the session to use */
986     if (greeter_get_guest_authenticated (greeter))
987     {
988         session = create_guest_session (seat, session_name);
989         if (!session)
990             return FALSE;
991         session_set_pam_service (session, AUTOLOGIN_SERVICE);
992     }
993     else
994     {
995         const gchar *language = NULL;
996         SessionConfig *session_config;
997         User *user;
998         gchar *sessions_dir = NULL;
999         gchar **argv;
1000
1001         session = greeter_get_authentication_session (greeter);
1002   
1003         /* Get session command to run */
1004         switch (type)
1005         {
1006         case SESSION_TYPE_LOCAL:
1007             sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
1008             break;
1009         case SESSION_TYPE_REMOTE:
1010             sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
1011             break;
1012         }
1013
1014         /* Load user preferences */
1015         user = session_get_user (session);
1016         if (user)
1017         {
1018             if (!session_name)
1019                 session_name = user_get_xsession (user);
1020             language = user_get_language (user);
1021         }
1022
1023         if (!session_name)
1024             session_name = seat_get_string_property (seat, "user-session");
1025         if (user)
1026             user_set_xsession (session_get_user (session), session_name);
1027
1028         session_config = find_session_config (seat, sessions_dir, session_name);
1029         g_free (sessions_dir);
1030         if (!session_config)
1031         {
1032             l_debug (seat, "Can't find session '%s'", seat_get_string_property (seat, "user-session"));
1033             return FALSE;
1034         }
1035
1036         configure_session (session, session_config, session_name, language);
1037         argv = get_session_argv (seat, session_config, seat_get_string_property (seat, "session-wrapper"));
1038         session_set_argv (session, argv);
1039         g_strfreev (argv);
1040         g_object_unref (session_config);
1041     }
1042
1043     /* Switch to this session when it is ready */
1044     if (seat->priv->session_to_activate)
1045         g_object_unref (seat->priv->session_to_activate);
1046     seat->priv->session_to_activate = g_object_ref (session);
1047
1048     /* Return to existing session if it is open */
1049     username = session_get_username (session);
1050     existing_session = find_user_session (seat, username, NULL);
1051     if (existing_session && session != existing_session)
1052     {
1053         l_debug (seat, "Returning to existing user session %s", username);
1054         session_stop (session);
1055         session_unlock (existing_session);
1056         seat_set_active_session (seat, existing_session);
1057         return TRUE;
1058     }
1059
1060     /* If can re-use the display server, stop the greeter first */
1061     display_server = session_get_display_server (SESSION (greeter));
1062     if (can_share_display_server (seat, display_server) &&
1063         strcmp (display_server_get_session_type (display_server), session_get_session_type (session)) == 0)
1064     {
1065         l_debug (seat, "Stopping greeter; display server will be re-used for user session");
1066
1067         /* Run on the same display server after the greeter has stopped */
1068         session_set_display_server (session, display_server);
1069
1070         /* Stop the greeter */
1071         session_stop (SESSION (greeter));
1072
1073         return TRUE;
1074     }
1075     /* Otherwise start a new display server for this session */
1076     else
1077     {
1078         display_server = create_display_server (seat, session_get_session_type (session));
1079         session_set_display_server (session, display_server);
1080         if (!display_server_start (display_server))
1081         {
1082             l_debug (seat, "Failed to start display server for new session");
1083             return FALSE;
1084         }
1085
1086         return TRUE;
1087     }
1088 }
1089
1090 static Greeter *
1091 create_greeter_session (Seat *seat)
1092 {
1093     gchar *sessions_dir, **argv;
1094     SessionConfig *session_config;
1095     Greeter *greeter_session;
1096     const gchar *greeter_wrapper;
1097
1098     l_debug (seat, "Creating greeter session");
1099
1100     sessions_dir = config_get_string (config_get_instance (), "LightDM", "greeters-directory");
1101     session_config = find_session_config (seat, sessions_dir, seat_get_string_property (seat, "greeter-session"));
1102     g_free (sessions_dir);
1103     if (!session_config)
1104         return NULL;
1105
1106     argv = get_session_argv (seat, session_config, NULL);
1107     greeter_wrapper = seat_get_string_property (seat, "greeter-wrapper");
1108     if (greeter_wrapper)
1109     {
1110         gchar *path;
1111         path = g_find_program_in_path (greeter_wrapper);
1112         prepend_argv (&argv, path ? path : greeter_wrapper);
1113         g_free (path);
1114     }
1115
1116     greeter_session = SEAT_GET_CLASS (seat)->create_greeter_session (seat);
1117     session_set_session_type (SESSION (greeter_session), session_config_get_session_type (session_config));
1118     seat->priv->sessions = g_list_append (seat->priv->sessions, SESSION (greeter_session));
1119     g_signal_connect (greeter_session, "notify::active-username", G_CALLBACK (greeter_active_username_changed_cb), seat);
1120     g_signal_connect (greeter_session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
1121     g_signal_connect (greeter_session, "stopped", G_CALLBACK (session_stopped_cb), seat);
1122   
1123     set_session_env (SESSION (greeter_session));
1124     session_set_env (SESSION (greeter_session), "XDG_SESSION_CLASS", "greeter");
1125
1126     session_set_pam_service (SESSION (greeter_session), GREETER_SERVICE);
1127     if (getuid () == 0)
1128     {
1129         gchar *greeter_user;      
1130         greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
1131         session_set_username (SESSION (greeter_session), greeter_user);
1132         g_free (greeter_user);
1133     }
1134     else
1135     {
1136         /* In test mode run the greeter as ourself */
1137         session_set_username (SESSION (greeter_session), user_get_name (accounts_get_current_user ()));
1138     }
1139     session_set_argv (SESSION (greeter_session), argv);
1140     g_strfreev (argv);
1141
1142     greeter_set_pam_services (greeter_session, USER_SERVICE, AUTOLOGIN_SERVICE);
1143     g_signal_connect (greeter_session, "create-session", G_CALLBACK (greeter_create_session_cb), seat);
1144     g_signal_connect (greeter_session, "start-session", G_CALLBACK (greeter_start_session_cb), seat);
1145
1146     /* Set hints to greeter */
1147     greeter_set_hint (greeter_session, "default-session", seat_get_string_property (seat, "user-session"));
1148     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
1149     greeter_set_hint (greeter_session, "hide-users", seat_get_boolean_property (seat, "greeter-hide-users") ? "true" : "false");
1150     greeter_set_hint (greeter_session, "show-manual-login", seat_get_boolean_property (seat, "greeter-show-manual-login") ? "true" : "false");
1151     greeter_set_hint (greeter_session, "show-remote-login", seat_get_boolean_property (seat, "greeter-show-remote-login") ? "true" : "false");
1152     greeter_set_hint (greeter_session, "has-guest-account", seat_get_allow_guest (seat) && seat_get_boolean_property (seat, "greeter-allow-guest") ? "true" : "false");
1153
1154     g_object_unref (session_config);
1155
1156     return greeter_session;
1157 }
1158
1159 static Session *
1160 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
1161 {
1162     GList *link;
1163
1164     for (link = seat->priv->sessions; link; link = link->next)
1165     {
1166         Session *session = link->data;
1167         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
1168             return session;
1169     }
1170
1171     return NULL;
1172 }
1173
1174 static void
1175 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
1176 {
1177     const gchar *script;
1178     Session *session;
1179
1180     /* Run setup script */
1181     script = seat_get_string_property (seat, "display-setup-script");
1182     if (script && !run_script (seat, display_server, script, NULL))
1183     {
1184         l_debug (seat, "Stopping display server due to failed setup script");
1185         display_server_stop (display_server);
1186         return;
1187     }
1188
1189     /* Stop if don't need to run a session */
1190     if (!get_start_local_sessions (seat))
1191         return;
1192
1193     emit_upstart_signal ("login-session-start");
1194
1195     /* Start the session waiting for this display server */
1196     session = find_session_for_display_server (seat, display_server);
1197     if (session)
1198     {
1199         if (session_get_is_authenticated (session))
1200         {
1201             l_debug (seat, "Display server ready, running session");
1202             run_session (seat, session);
1203         }
1204         else
1205         {
1206             l_debug (seat, "Display server ready, starting session authentication");
1207             start_session (seat, session);
1208         }
1209     }
1210     else
1211     {
1212         l_debug (seat, "Stopping not required display server");
1213         display_server_stop (display_server);
1214     }
1215 }
1216
1217 static DisplayServer *
1218 create_display_server (Seat *seat, const gchar *session_type)
1219 {
1220     DisplayServer *display_server;
1221
1222     l_debug (seat, "Creating display server of type %s", session_type);
1223
1224     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat, session_type);
1225     if (!display_server)
1226         return NULL;
1227
1228     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1229     g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
1230     g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_stopped_cb), seat);
1231
1232     return display_server;
1233 }
1234
1235 static Greeter *
1236 find_greeter_session (Seat *seat)
1237 {
1238     GList *link;
1239
1240     for (link = seat->priv->sessions; link; link = link->next)
1241     {
1242         Session *session = link->data;
1243         if (!session_get_is_stopping (session) && IS_GREETER (session))
1244             return GREETER (session);
1245     }
1246
1247     return NULL;
1248 }
1249
1250 gboolean
1251 seat_switch_to_greeter (Seat *seat)
1252 {
1253     Greeter *greeter_session;
1254     DisplayServer *display_server;
1255
1256     g_return_val_if_fail (seat != NULL, FALSE);
1257
1258     if (!seat->priv->can_switch)
1259         return FALSE;
1260
1261     /* Switch to greeter if one open (shouldn't be though) */
1262     greeter_session = find_greeter_session (seat);
1263     if (greeter_session)
1264     {
1265         l_debug (seat, "Switching to existing greeter");
1266         seat_set_active_session (seat, SESSION (greeter_session));
1267         return TRUE;
1268     }
1269
1270     greeter_session = create_greeter_session (seat);
1271     if (!greeter_session)
1272         return FALSE;
1273
1274     if (seat->priv->session_to_activate)
1275         g_object_unref (seat->priv->session_to_activate);
1276     seat->priv->session_to_activate = g_object_ref (greeter_session);
1277
1278     display_server = create_display_server (seat, session_get_session_type (SESSION (greeter_session)));
1279     session_set_display_server (SESSION (greeter_session), display_server);
1280     if (!display_server_start (display_server))
1281         return FALSE;
1282
1283     return TRUE;
1284 }
1285
1286 static void
1287 switch_authentication_complete_cb (Session *session, Seat *seat)
1288 {
1289     Greeter *greeter_session;
1290     DisplayServer *display_server;
1291
1292     /* If authenticated, then unlock existing session or start new one */
1293     if (session_get_is_authenticated (session))
1294     {
1295         Session *s;
1296
1297         s = find_user_session (seat, session_get_username (session), session);
1298         if (s)
1299         {
1300             l_debug (seat, "Session authenticated, switching to existing user session");
1301             session_unlock (s);
1302             seat_set_active_session (seat, s);
1303             session_stop (session);
1304         }
1305         else
1306         {
1307             l_debug (seat, "Session authenticated, starting display server");
1308             if (seat->priv->session_to_activate)
1309                 g_object_unref (seat->priv->session_to_activate);
1310             seat->priv->session_to_activate = g_object_ref (session);
1311             display_server = create_display_server (seat, session_get_session_type (session));
1312             session_set_display_server (session, display_server);
1313             display_server_start (display_server);
1314         }
1315
1316         return;
1317     }
1318
1319     l_debug (seat, "Switching to greeter to authenticate session");
1320
1321     session_stop (session);
1322
1323     greeter_session = create_greeter_session (seat);
1324     if (session_get_is_guest (session))
1325         greeter_set_hint (greeter_session, "select-guest", "true");
1326     else
1327         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
1328     if (seat->priv->session_to_activate)
1329         g_object_unref (seat->priv->session_to_activate);
1330     seat->priv->session_to_activate = g_object_ref (greeter_session);
1331
1332     display_server = create_display_server (seat, session_get_session_type (SESSION (greeter_session)));
1333     session_set_display_server (SESSION (greeter_session), display_server);
1334     display_server_start (display_server);
1335 }
1336
1337 gboolean
1338 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1339 {
1340     Session *session;
1341
1342     g_return_val_if_fail (seat != NULL, FALSE);
1343     g_return_val_if_fail (username != NULL, FALSE);
1344
1345     if (!seat->priv->can_switch)
1346         return FALSE;
1347
1348     /* If we're already on this session, then ignore */
1349     session = find_user_session (seat, username, NULL);
1350     if (session && session == seat->priv->active_session)
1351         return TRUE;
1352
1353     l_debug (seat, "Switching to user %s", username);
1354
1355     /* Attempt to authenticate them */
1356     session = create_user_session (seat, username, FALSE);
1357     g_signal_connect (session, "authentication-complete", G_CALLBACK (switch_authentication_complete_cb), seat);
1358     session_set_pam_service (session, USER_SERVICE);
1359     session_start (session);
1360
1361     return FALSE;
1362 }
1363
1364 static Session *
1365 find_guest_session (Seat *seat)
1366 {
1367     GList *link;
1368
1369     for (link = seat->priv->sessions; link; link = link->next)
1370     {
1371         Session *session = link->data;
1372         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1373             return session;
1374     }
1375
1376     return NULL;
1377 }
1378
1379 gboolean
1380 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1381 {
1382     Session *session;
1383     DisplayServer *display_server;
1384
1385     g_return_val_if_fail (seat != NULL, FALSE);
1386
1387     if (!seat->priv->can_switch || !seat_get_allow_guest (seat))
1388         return FALSE;
1389
1390     /* Switch to session if one open */
1391     session = find_guest_session (seat);
1392     if (session)
1393     {
1394         l_debug (seat, "Switching to existing guest account %s", session_get_username (session));
1395         seat_set_active_session (seat, session);
1396         return TRUE;
1397     }
1398
1399     session = create_guest_session (seat, session_name);
1400     if (!session)
1401         return FALSE;
1402
1403     display_server = create_display_server (seat, session_get_session_type (session));
1404
1405     if (seat->priv->session_to_activate)
1406         g_object_unref (seat->priv->session_to_activate);
1407     seat->priv->session_to_activate = g_object_ref (session);
1408     session_set_pam_service (session, AUTOLOGIN_SERVICE);
1409     session_set_display_server (session, display_server);
1410
1411     return display_server_start (display_server);
1412 }
1413
1414 gboolean
1415 seat_lock (Seat *seat, const gchar *username)
1416 {
1417     Greeter *greeter_session;
1418     DisplayServer *display_server;
1419
1420     g_return_val_if_fail (seat != NULL, FALSE);
1421
1422     if (!seat->priv->can_switch)
1423         return FALSE;
1424
1425     l_debug (seat, "Locking");
1426
1427     /* Switch to greeter if one open (shouldn't be though) */
1428     greeter_session = find_greeter_session (seat);
1429     if (greeter_session)
1430     {
1431         l_debug (seat, "Switching to existing greeter");
1432         seat_set_active_session (seat, SESSION (greeter_session));
1433         return TRUE;
1434     }
1435
1436     greeter_session = create_greeter_session (seat);
1437     if (!greeter_session)
1438         return FALSE;
1439
1440     display_server = create_display_server (seat, session_get_session_type (SESSION (greeter_session)));
1441
1442     if (seat->priv->session_to_activate)
1443         g_object_unref (seat->priv->session_to_activate);
1444     seat->priv->session_to_activate = g_object_ref (greeter_session);
1445     greeter_set_hint (greeter_session, "lock-screen", "true");
1446     if (username)
1447         greeter_set_hint (greeter_session, "select-user", username);
1448     session_set_display_server (SESSION (greeter_session), display_server);
1449
1450     return display_server_start (display_server);
1451 }
1452
1453 void
1454 seat_stop (Seat *seat)
1455 {
1456     g_return_if_fail (seat != NULL);
1457
1458     if (seat->priv->stopping)
1459         return;
1460
1461     l_debug (seat, "Stopping");
1462     seat->priv->stopping = TRUE;
1463     SEAT_GET_CLASS (seat)->stop (seat);
1464 }
1465
1466 gboolean
1467 seat_get_is_stopping (Seat *seat)
1468 {
1469     g_return_val_if_fail (seat != NULL, FALSE);
1470     return seat->priv->stopping;
1471 }
1472
1473 static gboolean
1474 seat_real_get_start_local_sessions (Seat *seat)
1475 {
1476     return TRUE;
1477 }
1478
1479 static void
1480 seat_real_setup (Seat *seat)
1481 {
1482 }
1483
1484 static gboolean
1485 seat_real_start (Seat *seat)
1486 {
1487     const gchar *autologin_username;
1488     int autologin_timeout;
1489     gboolean autologin_guest;
1490     gboolean autologin_in_background;
1491     Session *session = NULL, *background_session = NULL;
1492
1493     l_debug (seat, "Starting");
1494
1495     /* If this display server doesn't have a session running on it, just start it */
1496     if (!get_start_local_sessions (seat))
1497     {
1498         DisplayServer *display_server;
1499         display_server = create_display_server (seat, "x"); // FIXME: Not necessarily an X seat, but not sure what to put here
1500         return display_server_start (display_server);
1501     }
1502
1503     /* Get autologin settings */
1504     autologin_username = seat_get_string_property (seat, "autologin-user");
1505     if (g_strcmp0 (autologin_username, "") == 0)
1506         autologin_username = NULL;
1507     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1508     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1509     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1510
1511     /* Autologin if configured */
1512     if (autologin_timeout == 0 || autologin_in_background)
1513     {
1514         if (autologin_guest)
1515             session = create_guest_session (seat, NULL);
1516         else if (autologin_username != NULL)
1517             session = create_user_session (seat, autologin_username, TRUE);
1518
1519         if (session)
1520             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1521
1522         /* Load in background if required */
1523         if (autologin_in_background && session)
1524         {
1525             background_session = session;
1526             session = NULL;
1527         }
1528       
1529         if (session)
1530         {
1531             DisplayServer *display_server;
1532
1533             if (seat->priv->session_to_activate)
1534                 g_object_unref (seat->priv->session_to_activate);
1535             seat->priv->session_to_activate = g_object_ref (session);
1536
1537             display_server = create_display_server (seat, session_get_session_type (session));
1538             session_set_display_server (session, display_server);
1539             if (!display_server || !display_server_start (display_server))
1540             {
1541                 l_debug (seat, "Can't create display server for automatic login");
1542                 session_stop (session);
1543                 if (display_server)
1544                     display_server_stop (display_server);
1545                 session = NULL;
1546             }
1547         }
1548     }
1549
1550     /* Fallback to a greeter */
1551     if (!session)
1552     {
1553         Greeter *greeter_session;
1554         DisplayServer *display_server;
1555
1556         greeter_session = create_greeter_session (seat);
1557         if (!greeter_session)
1558         {
1559             l_debug (seat, "Failed to create greeter session");
1560             return FALSE;
1561         }
1562
1563         if (seat->priv->session_to_activate)
1564             g_object_unref (seat->priv->session_to_activate);
1565         seat->priv->session_to_activate = g_object_ref (greeter_session);
1566         session = SESSION (greeter_session);
1567
1568         if (autologin_timeout)
1569         {
1570             gchar *value;
1571
1572             value = g_strdup_printf ("%d", autologin_timeout);
1573             greeter_set_hint (greeter_session, "autologin-timeout", value);
1574             g_free (value);
1575             if (autologin_username)
1576                 greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1577             if (autologin_guest)
1578                 greeter_set_hint (greeter_session, "autologin-guest", "true");
1579         }
1580
1581         display_server = create_display_server (seat, session_get_session_type (session));
1582         session_set_display_server (session, display_server);
1583         if (!display_server || !display_server_start (display_server))
1584         {
1585             l_debug (seat, "Can't create display server for greeter");
1586             session_stop (session);
1587             if (display_server)
1588                 display_server_stop (display_server);
1589             session = NULL;
1590         }
1591     }
1592
1593     /* Fail if can't start a session */
1594     if (!session)
1595     {
1596         seat_stop (seat);
1597         return FALSE;
1598     }
1599
1600     /* Start background session */
1601     if (background_session)
1602     {
1603         DisplayServer *background_display_server;
1604
1605         background_display_server = create_display_server (seat, session_get_session_type (background_session));
1606         session_set_display_server (background_session, background_display_server);
1607         if (!display_server_start (background_display_server))
1608             l_warning (seat, "Failed to start display server for background session");
1609     }
1610
1611     seat->priv->started = TRUE;
1612     return TRUE;
1613 }
1614
1615 static Greeter *
1616 seat_real_create_greeter_session (Seat *seat)
1617 {
1618     return greeter_new ();
1619 }
1620
1621 static Session *
1622 seat_real_create_session (Seat *seat)
1623 {
1624     return session_new ();
1625 }
1626
1627 static void
1628 seat_real_set_active_session (Seat *seat, Session *session)
1629 {
1630 }
1631
1632 static void
1633 seat_real_set_next_session (Seat *seat, Session *session)
1634 {
1635 }
1636
1637 static Session *
1638 seat_real_get_active_session (Seat *seat)
1639 {
1640     return NULL;
1641 }
1642
1643 static void
1644 seat_real_stop (Seat *seat)
1645 {
1646     GList *list, *link;
1647
1648     check_stopped (seat);
1649     if (seat->priv->stopped)
1650         return;
1651
1652     /* Stop all the display servers and sessions on the seat. Copy the list as
1653      * it might be modified if a display server / session stops during this loop */
1654     list = g_list_copy (seat->priv->display_servers);
1655     for (link = list; link; link = link->next)
1656         g_object_ref (link->data);
1657     for (link = list; link; link = link->next)
1658     {
1659         DisplayServer *display_server = link->data;
1660         if (!display_server_get_is_stopping (display_server))
1661         {
1662             l_debug (seat, "Stopping display server");
1663             display_server_stop (display_server);
1664         }
1665     }
1666     g_list_free_full (list, g_object_unref);
1667     list = g_list_copy (seat->priv->sessions);
1668     for (link = list; link; link = link->next)
1669         g_object_ref (link->data);
1670     for (link = list; link; link = link->next)
1671     {
1672         Session *session = link->data;
1673         if (!session_get_is_stopping (session))
1674         {
1675             l_debug (seat, "Stopping session");
1676             session_stop (session);
1677         }
1678     }
1679     g_list_free_full (list, g_object_unref);
1680 }
1681
1682 static void
1683 seat_init (Seat *seat)
1684 {
1685     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1686     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1687     seat->priv->share_display_server = TRUE;
1688 }
1689
1690 static void
1691 seat_finalize (GObject *object)
1692 {
1693     Seat *self;
1694     GList *link;
1695
1696     self = SEAT (object);
1697
1698     g_hash_table_unref (self->priv->properties);
1699     for (link = self->priv->display_servers; link; link = link->next)
1700     {
1701         DisplayServer *display_server = link->data;
1702         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1703     }  
1704     g_list_free_full (self->priv->display_servers, g_object_unref);
1705     for (link = self->priv->sessions; link; link = link->next)
1706     {
1707         Session *session = link->data;
1708         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1709     }
1710     g_list_free_full (self->priv->sessions, g_object_unref);
1711     if (self->priv->active_session)
1712         g_object_unref (self->priv->active_session);
1713     if (self->priv->next_session)
1714         g_object_unref (self->priv->next_session);
1715     if (self->priv->session_to_activate)
1716         g_object_unref (self->priv->session_to_activate);
1717
1718     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1719 }
1720
1721 static void
1722 seat_class_init (SeatClass *klass)
1723 {
1724     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1725
1726     klass->get_start_local_sessions = seat_real_get_start_local_sessions;
1727     klass->setup = seat_real_setup;
1728     klass->start = seat_real_start;
1729     klass->create_greeter_session = seat_real_create_greeter_session;
1730     klass->create_session = seat_real_create_session;
1731     klass->set_active_session = seat_real_set_active_session;
1732     klass->get_active_session = seat_real_get_active_session;
1733     klass->set_next_session = seat_real_set_next_session;
1734     klass->run_script = seat_real_run_script;
1735     klass->stop = seat_real_stop;
1736
1737     object_class->finalize = seat_finalize;
1738
1739     g_type_class_add_private (klass, sizeof (SeatPrivate));
1740
1741     signals[SESSION_ADDED] =
1742         g_signal_new ("session-added",
1743                       G_TYPE_FROM_CLASS (klass),
1744                       G_SIGNAL_RUN_LAST,
1745                       G_STRUCT_OFFSET (SeatClass, session_added),
1746                       NULL, NULL,
1747                       NULL,
1748                       G_TYPE_NONE, 1, SESSION_TYPE);
1749     signals[RUNNING_USER_SESSION] =
1750         g_signal_new ("running-user-session",
1751                       G_TYPE_FROM_CLASS (klass),
1752                       G_SIGNAL_RUN_LAST,
1753                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1754                       NULL, NULL,
1755                       NULL,
1756                       G_TYPE_NONE, 1, SESSION_TYPE);
1757     signals[SESSION_REMOVED] =
1758         g_signal_new ("session-removed",
1759                       G_TYPE_FROM_CLASS (klass),
1760                       G_SIGNAL_RUN_LAST,
1761                       G_STRUCT_OFFSET (SeatClass, session_removed),
1762                       NULL, NULL,
1763                       NULL,
1764                       G_TYPE_NONE, 1, SESSION_TYPE);
1765     signals[STOPPED] =
1766         g_signal_new ("stopped",
1767                       G_TYPE_FROM_CLASS (klass),
1768                       G_SIGNAL_RUN_LAST,
1769                       G_STRUCT_OFFSET (SeatClass, stopped),
1770                       NULL, NULL,
1771                       NULL,
1772                       G_TYPE_NONE, 0);
1773 }
1774
1775 static gint
1776 seat_real_logprefix (Logger *self, gchar *buf, gulong buflen)
1777 {
1778     Seat *seat = SEAT (self);
1779     const gchar *name = seat_get_string_property (seat, "seat-name");
1780     if (name)
1781         return g_snprintf (buf, buflen, "Seat %s: ", name);
1782     else
1783         return g_snprintf (buf, buflen, "Seat: ");
1784 }
1785
1786 static void
1787 seat_logger_iface_init (LoggerInterface *iface)
1788 {
1789     iface->logprefix = &seat_real_logprefix;
1790 }