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