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