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