]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Kill the Display class and just use Seat. Display was a hang-over from the old way...
[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     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
971     g_signal_connect (greeter_session, "create-session", G_CALLBACK (greeter_create_session_cb), seat);
972     g_signal_connect (greeter_session, "start-session", G_CALLBACK (greeter_start_session_cb), seat);
973
974     return greeter_session;
975 }
976
977 static Session *
978 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
979 {
980     GList *link;
981
982     for (link = seat->priv->sessions; link; link = link->next)
983     {
984         Session *session = link->data;
985         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
986             return session;
987     }
988
989     return NULL;
990 }
991
992 static void
993 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
994 {
995     const gchar *script;
996     Session *session;
997
998     /* Run setup script */
999     script = seat_get_string_property (seat, "display-setup-script");
1000     if (script && !run_script (seat, display_server, script, NULL))
1001     {
1002         g_debug ("Stopping display server due to failed setup script");
1003         display_server_stop (display_server);
1004         return;
1005     }
1006
1007     /* Stop if don't need to run a session */
1008     if (!display_server_get_start_local_sessions (display_server))
1009         return;
1010
1011     emit_upstart_signal ("login-session-start");
1012
1013     /* Start the session waiting for this display server */
1014     session = find_session_for_display_server (seat, display_server);
1015     if (session)
1016     {
1017         if (session_get_is_authenticated (session))
1018         {
1019             g_debug ("Display server ready, running session");
1020             run_session (seat, session);
1021         }
1022         else
1023         {
1024             g_debug ("Display server ready, starting session authentication");
1025             start_session (seat, session);
1026         }
1027     }
1028     else
1029     {
1030         g_debug ("Stopping not required display server");
1031         display_server_stop (display_server);
1032     }
1033 }
1034
1035 static DisplayServer *
1036 create_display_server (Seat *seat)
1037 {
1038     DisplayServer *display_server;
1039
1040     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat);
1041     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1042     g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
1043     g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_stopped_cb), seat);
1044
1045     return display_server;
1046 }
1047
1048 static Greeter *
1049 find_greeter_session (Seat *seat)
1050 {
1051     GList *link;
1052
1053     for (link = seat->priv->sessions; link; link = link->next)
1054     {
1055         Session *session = link->data;
1056         if (!session_get_is_stopping (session) && IS_GREETER (session))
1057             return GREETER (session);
1058     }
1059
1060     return NULL;
1061 }
1062
1063 gboolean
1064 seat_switch_to_greeter (Seat *seat)
1065 {
1066     Greeter *greeter_session;
1067     DisplayServer *display_server;
1068
1069     g_return_val_if_fail (seat != NULL, FALSE);
1070
1071     if (!seat->priv->can_switch)
1072         return FALSE;
1073
1074     /* Switch to greeter if one open (shouldn't be though) */
1075     greeter_session = find_greeter_session (seat);
1076     if (greeter_session)
1077     {
1078         g_debug ("Switching to existing greeter");
1079         seat_set_active_session (seat, SESSION (greeter_session));
1080         return TRUE;
1081     }
1082
1083     greeter_session = create_greeter_session (seat);
1084     if (seat->priv->session_to_activate)
1085         g_object_unref (seat->priv->session_to_activate);
1086     seat->priv->session_to_activate = g_object_ref (greeter_session);
1087
1088     display_server = create_display_server (seat);
1089     session_set_display_server (SESSION (greeter_session), display_server);
1090     if (!display_server_start (display_server))
1091         return FALSE;
1092
1093     return TRUE;
1094 }
1095
1096 gboolean
1097 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1098 {
1099     Session *session;
1100     DisplayServer *display_server;
1101
1102     g_return_val_if_fail (seat != NULL, FALSE);
1103     g_return_val_if_fail (username != NULL, FALSE);
1104
1105     if (!seat->priv->can_switch)
1106         return FALSE;
1107
1108     g_debug ("Switching to user %s", username);
1109
1110     session = find_user_session (seat, username);
1111     if (session)
1112     {
1113         g_debug ("Switching to existing user session %s", username);
1114         seat_set_active_session (seat, session);
1115         return TRUE;
1116     }
1117
1118     session = create_user_session (seat, username);
1119     if (!session)
1120         return FALSE;
1121     if (seat->priv->session_to_activate)
1122         g_object_unref (seat->priv->session_to_activate);
1123     seat->priv->session_to_activate = g_object_ref (session);
1124     session_set_pam_service (session, USER_SERVICE);
1125
1126     display_server = create_display_server (seat);
1127     session_set_display_server (session, display_server);
1128     if (!display_server_start (display_server))
1129         return FALSE;
1130
1131     return FALSE;
1132 }
1133
1134 static Session *
1135 find_guest_session (Seat *seat)
1136 {
1137     GList *link;
1138
1139     for (link = seat->priv->sessions; link; link = link->next)
1140     {
1141         Session *session = link->data;
1142         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1143             return session;
1144     }
1145
1146     return NULL;
1147 }
1148
1149 gboolean
1150 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1151 {
1152     Session *session;
1153     DisplayServer *display_server;
1154     GList *link;
1155
1156     g_return_val_if_fail (seat != NULL, FALSE);
1157
1158     if (!seat->priv->can_switch || !seat_get_allow_guest (seat))
1159         return FALSE;
1160
1161     /* Switch to session if one open */
1162     session = find_guest_session (seat);
1163     if (session)
1164     {
1165         g_debug ("Switching to existing guest account %s", session_get_username (session));
1166         seat_set_active_session (seat, session);
1167         return TRUE;
1168     }
1169
1170     display_server = create_display_server (seat);
1171     if (!display_server_start (display_server))
1172         return FALSE;
1173
1174     session = create_guest_session (seat);
1175     if (!session)
1176         return FALSE;
1177     if (seat->priv->session_to_activate)
1178         g_object_unref (seat->priv->session_to_activate);
1179     seat->priv->session_to_activate = g_object_ref (session);
1180     session_set_pam_service (session, AUTOLOGIN_SERVICE);
1181     session_set_display_server (session, display_server);
1182
1183     return TRUE;
1184 }
1185
1186 gboolean
1187 seat_lock (Seat *seat, const gchar *username)
1188 {
1189     Greeter *greeter_session;
1190     DisplayServer *display_server;
1191
1192     g_return_val_if_fail (seat != NULL, FALSE);
1193
1194     if (!seat->priv->can_switch)
1195         return FALSE;
1196
1197     g_debug ("Locking seat");
1198
1199     /* Switch to greeter if one open (shouldn't be though) */
1200     greeter_session = find_greeter_session (seat);
1201     if (greeter_session)
1202     {
1203         g_debug ("Switching to existing greeter");
1204         seat_set_active_session (seat, SESSION (greeter_session));
1205         return TRUE;
1206     }
1207
1208     display_server = create_display_server (seat);
1209     if (!display_server_start (display_server))
1210         return FALSE;
1211
1212     greeter_session = create_greeter_session (seat);
1213     if (seat->priv->session_to_activate)
1214         g_object_unref (seat->priv->session_to_activate);
1215     seat->priv->session_to_activate = g_object_ref (greeter_session);
1216     greeter_set_hint (greeter_session, "lock-screen", "true");
1217     if (username)
1218         greeter_set_hint (greeter_session, "select-user", username);
1219     session_set_display_server (SESSION (greeter_session), display_server);
1220
1221     return TRUE;
1222 }
1223
1224 void
1225 seat_stop (Seat *seat)
1226 {
1227     g_return_if_fail (seat != NULL);
1228
1229     if (seat->priv->stopping)
1230         return;
1231
1232     g_debug ("Stopping seat");
1233     seat->priv->stopping = TRUE;
1234     SEAT_GET_CLASS (seat)->stop (seat);
1235 }
1236
1237 gboolean
1238 seat_get_is_stopping (Seat *seat)
1239 {
1240     g_return_val_if_fail (seat != NULL, FALSE);
1241     return seat->priv->stopping;
1242 }
1243
1244 static void
1245 seat_real_setup (Seat *seat)
1246 {
1247 }
1248
1249 static gboolean
1250 seat_real_start (Seat *seat)
1251 {
1252     const gchar *autologin_username;
1253     int autologin_timeout;
1254     gboolean autologin_guest;
1255     gboolean autologin_in_background;
1256     const gchar *user_session;
1257     Session *session = NULL;
1258     DisplayServer *display_server;
1259     User *user;
1260     gchar *sessions_dir;
1261     const gchar *wrapper = NULL;
1262     const gchar *session_name = NULL;
1263
1264     g_debug ("Starting seat");
1265
1266     display_server = create_display_server (seat);
1267
1268     /* If this display server doesn't have a session running on it, just start it */
1269     if (!display_server_get_start_local_sessions (display_server))
1270         return display_server_start (display_server);
1271
1272     /* Get autologin settings */
1273     autologin_username = seat_get_string_property (seat, "autologin-user");
1274     if (g_strcmp0 (autologin_username, "") == 0)
1275         autologin_username = NULL;
1276     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1277     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1278     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1279
1280     /* Autologin if configured */
1281     if (autologin_timeout == 0 || autologin_in_background)
1282     {
1283         if (autologin_guest)
1284             session = create_guest_session (seat);
1285         else if (autologin_username != NULL)
1286             session = create_user_session (seat, autologin_username);
1287       
1288         if (session)
1289         {
1290             if (seat->priv->session_to_activate)
1291                 g_object_unref (seat->priv->session_to_activate);
1292             seat->priv->session_to_activate = g_object_ref (session);
1293             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1294         }
1295
1296         /* Load in background if required */
1297         if (autologin_in_background && session)
1298         {
1299             DisplayServer *background_display_server;
1300
1301             background_display_server = create_display_server (seat);
1302             session_set_display_server (session, background_display_server);
1303             if (!display_server_start (background_display_server))
1304                 return FALSE;
1305
1306             /* Start a greeter as well */
1307             session = NULL;
1308         }
1309     }
1310
1311     /* Fallback to a greeter */
1312     if (!session)
1313     {
1314         Greeter *greeter_session;
1315
1316         greeter_session = create_greeter_session (seat);
1317         if (seat->priv->session_to_activate)
1318             g_object_unref (seat->priv->session_to_activate);
1319         seat->priv->session_to_activate = g_object_ref (greeter_session);
1320         session = SESSION (greeter_session);
1321
1322         if (autologin_timeout)
1323         {
1324             gchar *value;
1325
1326             value = g_strdup_printf ("%d", autologin_timeout);
1327             greeter_set_hint (greeter_session, "autologin-timeout", value);
1328             g_free (value);
1329             if (autologin_username)
1330                 greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1331             if (autologin_guest)
1332                 greeter_set_hint (greeter_session, "autologin-guest", "true");
1333         }
1334     }
1335
1336     /* Fail if can't start a session */
1337     if (!session)
1338     {
1339         seat_stop (seat);
1340         return FALSE;
1341     }
1342
1343     /* Start display server to show session on */
1344     session_set_display_server (session, display_server);
1345     if (!display_server_start (display_server))
1346         return FALSE;
1347
1348     return TRUE;
1349 }
1350
1351 static void
1352 seat_real_set_active_session (Seat *seat, Session *session)
1353 {
1354 }
1355
1356 static Session *
1357 seat_real_get_active_session (Seat *seat)
1358 {
1359     return NULL;
1360 }
1361
1362 static void
1363 seat_real_stop (Seat *seat)
1364 {
1365     GList *list, *link;
1366
1367     check_stopped (seat);
1368     if (seat->priv->stopped)
1369         return;
1370
1371     /* Stop all the display servers and sessions on the seat. Copy the list as
1372      * it might be modified if a display server / session stops during this loop */
1373     list = g_list_copy (seat->priv->display_servers);
1374     for (link = list; link; link = link->next)
1375         g_object_ref (link->data);
1376     for (link = list; link; link = link->next)
1377     {
1378         DisplayServer *display_server = link->data;
1379         if (!display_server_get_is_stopping (display_server))
1380         {
1381             g_debug ("Stopping display server");
1382             display_server_stop (display_server);
1383         }
1384     }
1385     g_list_free_full (list, g_object_unref);
1386     list = g_list_copy (seat->priv->sessions);
1387     for (link = list; link; link = link->next)
1388         g_object_ref (link->data);
1389     for (link = list; link; link = link->next)
1390     {
1391         Session *session = link->data;
1392         if (!session_get_is_stopping (session))
1393         {
1394             g_debug ("Stopping session");
1395             session_stop (session);
1396         }
1397     }
1398     g_list_free_full (list, g_object_unref);
1399 }
1400
1401 static void
1402 seat_init (Seat *seat)
1403 {
1404     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1405     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1406     seat->priv->share_display_server = TRUE;
1407 }
1408
1409 static void
1410 seat_finalize (GObject *object)
1411 {
1412     Seat *self;
1413     GList *link;
1414
1415     self = SEAT (object);
1416
1417     g_hash_table_unref (self->priv->properties);
1418     for (link = self->priv->display_servers; link; link = link->next)
1419     {
1420         DisplayServer *display_server = link->data;
1421         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1422     }  
1423     g_list_free_full (self->priv->display_servers, g_object_unref);
1424     for (link = self->priv->sessions; link; link = link->next)
1425     {
1426         Session *session = link->data;
1427         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1428     }
1429     g_list_free_full (self->priv->sessions, g_object_unref);
1430     if (self->priv->session_to_activate)
1431         g_object_unref (self->priv->session_to_activate);
1432
1433     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1434 }
1435
1436 static void
1437 seat_class_init (SeatClass *klass)
1438 {
1439     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1440
1441     klass->setup = seat_real_setup;
1442     klass->start = seat_real_start;
1443     klass->set_active_session = seat_real_set_active_session;
1444     klass->get_active_session = seat_real_get_active_session;
1445     klass->run_script = seat_real_run_script;
1446     klass->stop = seat_real_stop;
1447
1448     object_class->finalize = seat_finalize;
1449
1450     g_type_class_add_private (klass, sizeof (SeatPrivate));
1451
1452     signals[SESSION_ADDED] =
1453         g_signal_new ("session-added",
1454                       G_TYPE_FROM_CLASS (klass),
1455                       G_SIGNAL_RUN_LAST,
1456                       G_STRUCT_OFFSET (SeatClass, session_added),
1457                       NULL, NULL,
1458                       NULL,
1459                       G_TYPE_NONE, 1, SESSION_TYPE);
1460     signals[RUNNING_USER_SESSION] =
1461         g_signal_new ("running-user-session",
1462                       G_TYPE_FROM_CLASS (klass),
1463                       G_SIGNAL_RUN_LAST,
1464                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1465                       NULL, NULL,
1466                       NULL,
1467                       G_TYPE_NONE, 1, SESSION_TYPE);
1468     signals[SESSION_REMOVED] =
1469         g_signal_new ("session-removed",
1470                       G_TYPE_FROM_CLASS (klass),
1471                       G_SIGNAL_RUN_LAST,
1472                       G_STRUCT_OFFSET (SeatClass, session_removed),
1473                       NULL, NULL,
1474                       NULL,
1475                       G_TYPE_NONE, 1, SESSION_TYPE);
1476     signals[STOPPED] =
1477         g_signal_new ("stopped",
1478                       G_TYPE_FROM_CLASS (klass),
1479                       G_SIGNAL_RUN_LAST,
1480                       G_STRUCT_OFFSET (SeatClass, stopped),
1481                       NULL, NULL,
1482                       NULL,
1483                       G_TYPE_NONE, 0);
1484 }