]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Merge with trunk
[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)
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     /* Variables required for regression tests */
582     if (g_getenv ("LIGHTDM_TEST_ROOT"))
583     {
584         session_set_env (session, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
585         session_set_env (session, "DBUS_SYSTEM_BUS_ADDRESS", g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
586         session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
587         session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
588         session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
589         session_set_env (session, "GI_TYPELIB_PATH", g_getenv ("GI_TYPELIB_PATH"));
590     }
591 }
592
593 static Session *
594 create_session (Seat *seat, gboolean autostart)
595 {
596     Session *session;
597
598     session = SEAT_GET_CLASS (seat)->create_session (seat);
599     seat->priv->sessions = g_list_append (seat->priv->sessions, session);
600     if (autostart)
601         g_signal_connect (session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
602     g_signal_connect (session, "stopped", G_CALLBACK (session_stopped_cb), seat);
603
604     set_session_env (session);
605
606     g_signal_emit (seat, signals[SESSION_ADDED], 0, session);
607
608     return session;
609 }
610
611 static gchar **
612 get_session_argv_from_filename (const gchar *filename, const gchar *session_wrapper)
613 {
614     GKeyFile *session_desktop_file;
615     gboolean result;
616     int argc;
617     gchar *command = NULL, **argv, *path;
618     GError *error = NULL;
619
620     /* Read the command from the .desktop file */
621     session_desktop_file = g_key_file_new ();
622     result = g_key_file_load_from_file (session_desktop_file, filename, G_KEY_FILE_NONE, &error);
623     if (error)
624         g_debug ("Failed to load session file %s: %s", filename, error->message);
625     g_clear_error (&error);
626     if (result)
627     {
628         command = g_key_file_get_string (session_desktop_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
629         if (!command)
630             g_debug ("No command in session file %s", filename);
631     }
632     g_key_file_free (session_desktop_file);
633
634     if (!command)
635         return NULL;
636
637     /* If configured, run sessions through a wrapper */
638     if (session_wrapper)
639     {
640         argv = g_malloc (sizeof (gchar *) * 3);
641         path = g_find_program_in_path (session_wrapper);
642         argv[0] = path ? path : g_strdup (session_wrapper);
643         argv[1] = command;
644         argv[2] = NULL;
645         return argv;
646     }
647
648     /* Split command into an array listing and make command absolute */
649     result = g_shell_parse_argv (command, &argc, &argv, &error);
650     if (error)
651         g_debug ("Invalid session command '%s': %s", command, error->message);
652     g_clear_error (&error);
653     g_free (command);
654     if (!result)
655         return NULL;
656     path = g_find_program_in_path (argv[0]);
657     if (path)
658     {
659         g_free (argv[0]);
660         argv[0] = path;
661     }
662   
663     return argv;
664 }
665
666 static gchar **
667 get_session_argv (const gchar *sessions_dir, const gchar *session_name, const gchar *session_wrapper)
668 {
669     gchar **dirs, **argv;
670     int i;
671
672     g_return_val_if_fail (sessions_dir != NULL, NULL);
673     g_return_val_if_fail (session_name != NULL, NULL);
674
675     dirs = g_strsplit (sessions_dir, ":", -1);
676     for (i = 0; dirs[i]; i++)
677     {
678         gchar *filename, *path;
679
680         filename = g_strdup_printf ("%s.desktop", session_name);
681         path = g_build_filename (dirs[i], filename, NULL);
682         g_free (filename);
683         argv = get_session_argv_from_filename (path, session_wrapper);
684         g_free (path);
685         if (argv)
686             break;
687     }
688     g_strfreev (dirs);
689
690     return argv;
691 }
692
693 static Session *
694 create_user_session (Seat *seat, const gchar *username)
695 {
696     User *user;
697     gchar *sessions_dir, **argv;
698     const gchar *session_name;
699     Session *session;
700   
701     user = accounts_get_user_by_name (username);
702     if (!user)
703     {
704         g_debug ("Can't login unknown user '%s'", username);
705         return NULL;
706     }
707
708     session_name = user_get_xsession (user);
709     g_object_unref (user);
710     if (!session_name)
711         session_name = seat_get_string_property (seat, "user-session");
712     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
713     argv = get_session_argv (sessions_dir, session_name, seat_get_string_property (seat, "session-wrapper"));
714     g_free (sessions_dir);
715     if (!argv)
716     {
717         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
718         return NULL;
719     }
720
721     session = create_session (seat, TRUE);
722     session_set_pam_service (session, AUTOLOGIN_SERVICE);
723     session_set_username (session, username);
724     session_set_do_authenticate (session, TRUE);
725     session_set_argv (session, argv);
726
727     return session;
728 }
729
730 static Session *
731 create_guest_session (Seat *seat)
732 {
733     gchar *sessions_dir, **argv;
734     Session *session;
735
736     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
737     argv = get_session_argv (sessions_dir,
738                              seat_get_string_property (seat, "user-session"),
739                              seat_get_string_property (seat, "session-wrapper"));
740     g_free (sessions_dir);
741     if (!argv)
742     {
743         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
744         return NULL;
745     }
746
747     session = create_session (seat, TRUE);
748     session_set_do_authenticate (session, TRUE);
749     session_set_is_guest (session, TRUE);
750     session_set_argv (session, argv);
751   
752     return session;
753 }
754
755 static Session *
756 greeter_create_session_cb (Greeter *greeter, Seat *seat)
757 {
758     Session *session;
759
760     session = create_session (seat, FALSE);
761     session_set_display_server (session, session_get_display_server (SESSION (greeter)));
762
763     return g_object_ref (session);
764 }
765
766 static void
767 prepend_argv (gchar ***argv, const gchar *value)
768 {
769     gchar **old_argv, **new_argv;
770     gint i;
771
772     old_argv = *argv;
773     new_argv = g_malloc (sizeof (gchar *) * (g_strv_length (*argv) + 2));
774     new_argv[0] = g_strdup (value);
775     for (i = 0; old_argv[i]; i++)
776         new_argv[i + 1] = old_argv[i];
777     new_argv[i + 1] = NULL;
778
779     g_free (*argv);
780     *argv = new_argv;
781 }
782
783 static Session *
784 find_user_session (Seat *seat, const gchar *username)
785 {
786     GList *link;
787
788     if (!username)
789         return NULL;
790
791     for (link = seat->priv->sessions; link; link = link->next)
792     {
793         Session *session = link->data;
794
795         if (!session_get_is_stopping (session) && strcmp (session_get_username (session), username) == 0)
796             return session;
797     }
798
799     return NULL;
800 }
801
802 static gboolean
803 greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *session_name, Seat *seat)
804 {
805     Session *session, *existing_session;
806     const gchar *username;
807     User *user;
808     gchar *sessions_dir = NULL;
809     gchar **argv;
810
811     /* Get the session to use */
812     if (greeter_get_guest_authenticated (greeter))
813     {
814         session = create_guest_session (seat);
815         session_set_pam_service (session, AUTOLOGIN_SERVICE);
816     }
817     else
818         session = greeter_get_authentication_session (greeter);
819
820     /* Switch to this session when it is ready */
821     if (seat->priv->session_to_activate)
822         g_object_unref (seat->priv->session_to_activate);
823     seat->priv->session_to_activate = g_object_ref (session);
824
825     /* Return to existing session if it is open */
826     username = session_get_username (session);
827     existing_session = find_user_session (seat, username);
828     if (existing_session && session != existing_session)
829     {
830         g_debug ("Returning to existing user session %s", username);
831         session_stop (session);
832         seat_set_active_session (seat, existing_session);
833         return TRUE;
834     }
835
836     /* Get session command to run */
837     switch (type)
838     {
839     case SESSION_TYPE_LOCAL:
840         sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
841         break;
842     case SESSION_TYPE_REMOTE:
843         sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
844         break;
845     }
846     user = session_get_user (session);
847     if (!session_name && user)
848         session_name = user_get_xsession (user);
849     if (!session_name)
850         session_name = seat_get_string_property (seat, "user-session");
851     if (user)
852         user_set_xsession (session_get_user (session), session_name);
853     argv = get_session_argv (sessions_dir, session_name, seat_get_string_property (seat, "session-wrapper"));
854     g_free (sessions_dir);
855   
856     if (!argv)
857     {
858         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
859         return FALSE;
860     }
861
862     session_set_argv (session, argv);
863     g_strfreev (argv);
864
865     /* If no session information found, then can't start the session */
866     if (!argv)
867     {
868         g_debug ("Can't find greeter session '%s'", session_name);
869         return FALSE;
870     }
871
872     /* If can re-use the display server, stop the greeter first */
873     if (seat->priv->share_display_server)
874     {
875         /* Run on the same display server after the greeter has stopped */
876         session_set_display_server (session, session_get_display_server (SESSION (greeter)));
877
878         g_debug ("Stopping greeter");
879         session_stop (SESSION (greeter));
880
881         return TRUE;
882     }
883     /* Otherwise start a new display server for this session */
884     else
885     {
886         DisplayServer *display_server;
887
888         display_server = create_display_server (seat);
889         if (!display_server_start (display_server))
890             return FALSE;
891
892         session_set_display_server (session, display_server);
893
894         return TRUE;
895     }
896 }
897
898 static Greeter *
899 create_greeter_session (Seat *seat)
900 {
901     gchar *sessions_dir, **argv;
902     Greeter *greeter_session;
903     gchar *greeter_user;
904     const gchar *greeter_wrapper;
905
906     sessions_dir = config_get_string (config_get_instance (), "LightDM", "greeters-directory");
907     argv = get_session_argv (sessions_dir,
908                              seat_get_string_property (seat, "greeter-session"),
909                              seat_get_string_property (seat, "session-wrapper"));
910     g_free (sessions_dir);
911     if (!argv)
912         return NULL;
913
914     greeter_wrapper = seat_get_string_property (seat, "greeter-wrapper");
915     if (greeter_wrapper)
916     {
917         gchar *path;
918         path = g_find_program_in_path (greeter_wrapper);
919         prepend_argv (&argv, path ? path : greeter_wrapper);
920         g_free (path);
921     }
922
923     greeter_session = SEAT_GET_CLASS (seat)->create_greeter_session (seat);
924     seat->priv->sessions = g_list_append (seat->priv->sessions, SESSION (greeter_session));
925     g_signal_connect (greeter_session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
926     g_signal_connect (greeter_session, "stopped", G_CALLBACK (session_stopped_cb), seat);
927   
928     set_session_env (SESSION (greeter_session));
929
930     session_set_pam_service (SESSION (greeter_session), GREETER_SERVICE);
931     greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
932     session_set_username (SESSION (greeter_session), greeter_user);
933     g_free (greeter_user);
934     session_set_argv (SESSION (greeter_session), argv);
935
936     greeter_set_pam_services (greeter_session, USER_SERVICE, AUTOLOGIN_SERVICE);
937     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
938     g_signal_connect (greeter_session, "create-session", G_CALLBACK (greeter_create_session_cb), seat);
939     g_signal_connect (greeter_session, "start-session", G_CALLBACK (greeter_start_session_cb), seat);
940
941     return greeter_session;
942 }
943
944 static Session *
945 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
946 {
947     GList *link;
948
949     for (link = seat->priv->sessions; link; link = link->next)
950     {
951         Session *session = link->data;
952         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
953             return session;
954     }
955
956     return NULL;
957 }
958
959 static void
960 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
961 {
962     const gchar *script;
963     Session *session;
964
965     /* Run setup script */
966     script = seat_get_string_property (seat, "display-setup-script");
967     if (script && !run_script (seat, display_server, script, NULL))
968     {
969         g_debug ("Stopping display server due to failed setup script");
970         display_server_stop (display_server);
971         return;
972     }
973
974     emit_upstart_signal ("login-session-start");
975
976     /* Start the session waiting for this display server */
977     session = find_session_for_display_server (seat, display_server);
978     if (session)
979     {
980         if (session_get_is_authenticated (session))
981         {
982             g_debug ("Display server ready, running session");
983             run_session (seat, session);
984         }
985         else
986         {
987             g_debug ("Display server ready, starting session authentication");
988             start_session (seat, session);
989         }
990     }
991     else
992     {
993         g_debug ("Stopping not required display server");
994         display_server_stop (display_server);
995     }
996 }
997
998 static DisplayServer *
999 create_display_server (Seat *seat)
1000 {
1001     DisplayServer *display_server;
1002
1003     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat);
1004     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1005     g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
1006     g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_stopped_cb), seat);
1007
1008     return display_server;
1009 }
1010
1011 static Greeter *
1012 find_greeter_session (Seat *seat)
1013 {
1014     GList *link;
1015
1016     for (link = seat->priv->sessions; link; link = link->next)
1017     {
1018         Session *session = link->data;
1019         if (!session_get_is_stopping (session) && IS_GREETER (session))
1020             return GREETER (session);
1021     }
1022
1023     return NULL;
1024 }
1025
1026 gboolean
1027 seat_switch_to_greeter (Seat *seat)
1028 {
1029     Greeter *greeter_session;
1030     DisplayServer *display_server;
1031
1032     g_return_val_if_fail (seat != NULL, FALSE);
1033
1034     if (!seat->priv->can_switch)
1035         return FALSE;
1036
1037     /* Switch to greeter if one open (shouldn't be though) */
1038     greeter_session = find_greeter_session (seat);
1039     if (greeter_session)
1040     {
1041         g_debug ("Switching to existing greeter");
1042         seat_set_active_session (seat, SESSION (greeter_session));
1043         return TRUE;
1044     }
1045
1046     greeter_session = create_greeter_session (seat);
1047     if (seat->priv->session_to_activate)
1048         g_object_unref (seat->priv->session_to_activate);
1049     seat->priv->session_to_activate = g_object_ref (greeter_session);
1050
1051     display_server = create_display_server (seat);
1052     session_set_display_server (SESSION (greeter_session), display_server);
1053     if (!display_server_start (display_server))
1054         return FALSE;
1055
1056     return TRUE;
1057 }
1058
1059 gboolean
1060 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1061 {
1062     Session *session;
1063     DisplayServer *display_server;
1064
1065     g_return_val_if_fail (seat != NULL, FALSE);
1066     g_return_val_if_fail (username != NULL, FALSE);
1067
1068     if (!seat->priv->can_switch)
1069         return FALSE;
1070
1071     g_debug ("Switching to user %s", username);
1072
1073     session = find_user_session (seat, username);
1074     if (session)
1075     {
1076         g_debug ("Switching to existing user session %s", username);
1077         seat_set_active_session (seat, session);
1078         return TRUE;
1079     }
1080
1081     session = create_user_session (seat, username);
1082     if (seat->priv->session_to_activate)
1083         g_object_unref (seat->priv->session_to_activate);
1084     seat->priv->session_to_activate = g_object_ref (session);
1085     session_set_pam_service (session, USER_SERVICE);
1086
1087     display_server = create_display_server (seat);
1088     session_set_display_server (session, display_server);
1089     if (!display_server_start (display_server))
1090         return FALSE;
1091
1092     return FALSE;
1093 }
1094
1095 static Session *
1096 find_guest_session (Seat *seat)
1097 {
1098     GList *link;
1099
1100     for (link = seat->priv->sessions; link; link = link->next)
1101     {
1102         Session *session = link->data;
1103         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1104             return session;
1105     }
1106
1107     return NULL;
1108 }
1109
1110 gboolean
1111 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1112 {
1113     Session *session;
1114     DisplayServer *display_server;
1115     GList *link;
1116
1117     g_return_val_if_fail (seat != NULL, FALSE);
1118
1119     if (!seat->priv->can_switch || !seat_get_allow_guest (seat))
1120         return FALSE;
1121
1122     /* Switch to session if one open */
1123     session = find_guest_session (seat);
1124     if (session)
1125     {
1126         g_debug ("Switching to existing guest account %s", session_get_username (session));
1127         seat_set_active_session (seat, session);
1128         return TRUE;
1129     }
1130
1131     display_server = create_display_server (seat);
1132     if (!display_server_start (display_server))
1133         return FALSE;
1134
1135     session = create_guest_session (seat);
1136     if (seat->priv->session_to_activate)
1137         g_object_unref (seat->priv->session_to_activate);
1138     seat->priv->session_to_activate = g_object_ref (session);
1139     session_set_pam_service (session, AUTOLOGIN_SERVICE);
1140     session_set_display_server (session, display_server);
1141
1142     return TRUE;
1143 }
1144
1145 gboolean
1146 seat_lock (Seat *seat, const gchar *username)
1147 {
1148     Greeter *greeter_session;
1149     DisplayServer *display_server;
1150
1151     g_return_val_if_fail (seat != NULL, FALSE);
1152
1153     if (!seat->priv->can_switch)
1154         return FALSE;
1155
1156     g_debug ("Locking seat");
1157
1158     /* Switch to greeter if one open (shouldn't be though) */
1159     greeter_session = find_greeter_session (seat);
1160     if (greeter_session)
1161     {
1162         g_debug ("Switching to existing greeter");
1163         seat_set_active_session (seat, SESSION (greeter_session));
1164         return TRUE;
1165     }
1166
1167     display_server = create_display_server (seat);
1168     if (!display_server_start (display_server))
1169         return FALSE;
1170
1171     greeter_session = create_greeter_session (seat);
1172     if (seat->priv->session_to_activate)
1173         g_object_unref (seat->priv->session_to_activate);
1174     seat->priv->session_to_activate = g_object_ref (greeter_session);
1175     greeter_set_hint (greeter_session, "lock-screen", "true");
1176     if (username)
1177         greeter_set_hint (greeter_session, "select-user", username);
1178     session_set_display_server (SESSION (greeter_session), display_server);
1179
1180     return TRUE;
1181 }
1182
1183 void
1184 seat_stop (Seat *seat)
1185 {
1186     g_return_if_fail (seat != NULL);
1187
1188     if (seat->priv->stopping)
1189         return;
1190
1191     g_debug ("Stopping seat");
1192     seat->priv->stopping = TRUE;
1193     SEAT_GET_CLASS (seat)->stop (seat);
1194 }
1195
1196 gboolean
1197 seat_get_is_stopping (Seat *seat)
1198 {
1199     g_return_val_if_fail (seat != NULL, FALSE);
1200     return seat->priv->stopping;
1201 }
1202
1203 static void
1204 seat_real_setup (Seat *seat)
1205 {
1206 }
1207
1208 static gboolean
1209 seat_real_start (Seat *seat)
1210 {
1211     const gchar *autologin_username;
1212     int autologin_timeout;
1213     gboolean autologin_guest;
1214     gboolean autologin_in_background;
1215     const gchar *user_session;
1216     Session *session = NULL;
1217     DisplayServer *display_server;
1218     User *user;
1219     gchar *sessions_dir;
1220     const gchar *wrapper = NULL;
1221     const gchar *session_name = NULL;
1222
1223     g_debug ("Starting seat");
1224
1225     display_server = create_display_server (seat);
1226
1227     /* Get autologin settings */
1228     autologin_username = seat_get_string_property (seat, "autologin-user");
1229     if (g_strcmp0 (autologin_username, "") == 0)
1230         autologin_username = NULL;
1231     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1232     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1233     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1234
1235     /* Autologin if configured */
1236     if (autologin_timeout == 0 || autologin_in_background)
1237     {
1238         if (autologin_guest)
1239         {
1240             session = create_guest_session (seat);
1241             if (seat->priv->session_to_activate)
1242                 g_object_unref (seat->priv->session_to_activate);
1243             seat->priv->session_to_activate = g_object_ref (session);
1244             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1245         }
1246         else if (autologin_username != NULL)
1247         {
1248             session = create_user_session (seat, autologin_username);
1249             if (seat->priv->session_to_activate)
1250                 g_object_unref (seat->priv->session_to_activate);
1251             seat->priv->session_to_activate = g_object_ref (session);
1252             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1253         }
1254
1255         /* Load in background if required */
1256         if (autologin_in_background && session)
1257         {
1258             DisplayServer *background_display_server;
1259
1260             background_display_server = create_display_server (seat);
1261             session_set_display_server (session, background_display_server);
1262             if (!display_server_start (background_display_server))
1263                 return FALSE;
1264
1265             /* Start a greeter as well */
1266             session = NULL;
1267         }
1268     }
1269
1270     /* Fallback to a greeter */
1271     if (!session)
1272     {
1273         Greeter *greeter_session;
1274
1275         greeter_session = create_greeter_session (seat);
1276         if (seat->priv->session_to_activate)
1277             g_object_unref (seat->priv->session_to_activate);
1278         seat->priv->session_to_activate = g_object_ref (greeter_session);
1279         session = SESSION (greeter_session);
1280
1281         if (autologin_timeout)
1282         {
1283             gchar *value;
1284
1285             value = g_strdup_printf ("%d", autologin_timeout);
1286             greeter_set_hint (greeter_session, "autologin-timeout", value);
1287             g_free (value);
1288             if (autologin_username)
1289                 greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1290             if (autologin_guest)
1291                 greeter_set_hint (greeter_session, "autologin-guest", "true");
1292         }
1293     }
1294
1295     /* Fail if can't start a session */
1296     if (!session)
1297     {
1298         seat_stop (seat);
1299         return FALSE;
1300     }
1301
1302     /* Start display server to show session on */
1303     session_set_display_server (session, display_server);
1304     if (!display_server_start (display_server))
1305         return FALSE;
1306
1307     return TRUE;
1308 }
1309
1310 static void
1311 seat_real_set_active_session (Seat *seat, Session *session)
1312 {
1313 }
1314
1315 static Session *
1316 seat_real_get_active_session (Seat *seat)
1317 {
1318     return NULL;
1319 }
1320
1321 static void
1322 seat_real_stop (Seat *seat)
1323 {
1324     GList *list, *link;
1325
1326     check_stopped (seat);
1327     if (seat->priv->stopped)
1328         return;
1329
1330     /* Stop all the display servers and sessions on the seat. Copy the list as
1331      * it might be modified if a display server / session stops during this loop */
1332     list = g_list_copy (seat->priv->display_servers);
1333     for (link = list; link; link = link->next)
1334         g_object_ref (link->data);
1335     for (link = list; link; link = link->next)
1336     {
1337         DisplayServer *display_server = link->data;
1338         if (!display_server_get_is_stopping (display_server))
1339         {
1340             g_debug ("Stopping display server");
1341             display_server_stop (display_server);
1342         }
1343     }
1344     g_list_free_full (list, g_object_unref);
1345     list = g_list_copy (seat->priv->sessions);
1346     for (link = list; link; link = link->next)
1347         g_object_ref (link->data);
1348     for (link = list; link; link = link->next)
1349     {
1350         Session *session = link->data;
1351         if (!session_get_is_stopping (session))
1352         {
1353             g_debug ("Stopping session");
1354             session_stop (session);
1355         }
1356     }
1357     g_list_free_full (list, g_object_unref);
1358 }
1359
1360 static void
1361 seat_init (Seat *seat)
1362 {
1363     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1364     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1365     seat->priv->share_display_server = TRUE;
1366 }
1367
1368 static void
1369 seat_finalize (GObject *object)
1370 {
1371     Seat *self;
1372     GList *link;
1373
1374     self = SEAT (object);
1375
1376     g_hash_table_unref (self->priv->properties);
1377     for (link = self->priv->display_servers; link; link = link->next)
1378     {
1379         DisplayServer *display_server = link->data;
1380         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1381     }  
1382     g_list_free_full (self->priv->display_servers, g_object_unref);
1383     for (link = self->priv->sessions; link; link = link->next)
1384     {
1385         Session *session = link->data;
1386         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1387     }
1388     g_list_free_full (self->priv->sessions, g_object_unref);
1389     if (self->priv->session_to_activate)
1390         g_object_unref (self->priv->session_to_activate);
1391
1392     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1393 }
1394
1395 static void
1396 seat_class_init (SeatClass *klass)
1397 {
1398     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1399
1400     klass->setup = seat_real_setup;
1401     klass->start = seat_real_start;
1402     klass->set_active_session = seat_real_set_active_session;
1403     klass->get_active_session = seat_real_get_active_session;
1404     klass->run_script = seat_real_run_script;
1405     klass->stop = seat_real_stop;
1406
1407     object_class->finalize = seat_finalize;
1408
1409     g_type_class_add_private (klass, sizeof (SeatPrivate));
1410
1411     signals[SESSION_ADDED] =
1412         g_signal_new ("session-added",
1413                       G_TYPE_FROM_CLASS (klass),
1414                       G_SIGNAL_RUN_LAST,
1415                       G_STRUCT_OFFSET (SeatClass, session_added),
1416                       NULL, NULL,
1417                       NULL,
1418                       G_TYPE_NONE, 1, SESSION_TYPE);
1419     signals[RUNNING_USER_SESSION] =
1420         g_signal_new ("running-user-session",
1421                       G_TYPE_FROM_CLASS (klass),
1422                       G_SIGNAL_RUN_LAST,
1423                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1424                       NULL, NULL,
1425                       NULL,
1426                       G_TYPE_NONE, 1, SESSION_TYPE);
1427     signals[SESSION_REMOVED] =
1428         g_signal_new ("session-removed",
1429                       G_TYPE_FROM_CLASS (klass),
1430                       G_SIGNAL_RUN_LAST,
1431                       G_STRUCT_OFFSET (SeatClass, session_removed),
1432                       NULL, NULL,
1433                       NULL,
1434                       G_TYPE_NONE, 1, SESSION_TYPE);
1435     signals[STOPPED] =
1436         g_signal_new ("stopped",
1437                       G_TYPE_FROM_CLASS (klass),
1438                       G_SIGNAL_RUN_LAST,
1439                       G_STRUCT_OFFSET (SeatClass, stopped),
1440                       NULL, NULL,
1441                       NULL,
1442                       G_TYPE_NONE, 0);
1443 }