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