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